찾다
데이터 베이스MySQL 튜토리얼【一】仿微信飞机大战cocos2d

参考 【偶尔e网事】 的 【cocos2d-x入门实战】微信飞机大战 cocos2dx 2.0版本, 偶尔e网事他 写的非常详细,面面俱到,大家非常有必要看下。可以通过下面链接跳转: cocos2d-x入门实战 这里面我以 【cocos2d-x入门实战】微信飞机大战为蓝本,用cocos2dx 3.0r

参考 【偶尔e网事】 的 【cocos2d-x入门实战】微信飞机大战  cocos2dx 2.0版本,偶尔e网事他写的非常详细,面面俱到,大家非常有必要看下。可以通过下面链接跳转:

cocos2d-x入门实战

这里面我以【cocos2d-x入门实战】微信飞机大战 为蓝本,用cocos2dx 3.0rc1翻版。安装环境什么的,我就不说了,网上都可以找到,我直接从游戏开始界面说起。

想往下看的话,你必须会的一件事,就是你已经能创建出cocos2dx3.rc1的helloworld工程。

飞机大战源码和资源放在第四节中,不想看的直接去第四节中找吧


打飞机是一项需要前戏的运动,所以我们加个欢迎界面什么的,搞搞前戏气氛,还是很有必要的。

下面就让我们完成前戏,该做的事情:

1.游戏开始界面


一、首先是开始欢迎界面的展示

【一】仿微信飞机大战cocos2d

这里我们实现了简单静态界面,以及一个炫酷的动态图,虽然只是三秒钟!【一】仿微信飞机大战cocos2d,我这里直接用了偶尔e网事大神的资源,大神请原谅我把你的飞机升级成3.0版本的,如果不爽,请过来打我~好吧,我好jian.....


二、初始工程的介绍

假设你已经创建了一个名为“PlayThePlane”的工程,那么你的解决方案将会是这样的:

【一】仿微信飞机大战cocos2d


工程是从main.cpp开始执行的:

int APIENTRY _tWinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPTSTR    lpCmdLine,
                       int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);// UNREFERENCED_PARAMETER 告诉编译器,已经使用了该变量,不必检测警告!
    UNREFERENCED_PARAMETER(lpCmdLine);    // 要是没加,应该会有这个“warning C4100: “lpCmdLine” : unreferenced formal parameter.”

    // create the application instance 创建应用实例
    AppDelegate app;
    return Application::getInstance()->run();// cocos2dx AppDelegate程序正式开始运行
}
Application::getInstance()->run()里面到底运行了什么呢?混蛋,自己跳进去看下不就知道了,又不是陷阱,那可都是宝藏堆。我只告诉你它调用了AppDelegate.h中的applicationDidFinishLaunching();这时候我们看看
bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director 导演
    auto director = Director::getInstance();

	// 窗体框架
    auto glview = director->getOpenGLView();
    if(!glview) {
        glview = GLView::createWithRect("PlayerThePlane", Rect(0, 0, 480, 800)); // 窗体名 + 宽高规格
        director->setOpenGLView(glview);

		// 1.LOOK 该函数会自动按设计宽高和缩放方式适应手机屏幕,设置游戏分辨率 (设计宽,设计高,缩放方式)。
		glview->setDesignResolutionSize(480, 800, kResolutionNoBorder); 
    }

    // turn on display FPS 打印帧率,不希望左下角三行出现的 就注释掉 或者设置false
    director->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call this 一秒60帧
    director->setAnimationInterval(1.0 / 60);

    // create a scene. it's an autorelease object 创建场景
    auto scene = HelloWorld::createScene();

    // run 导演让场景开始运作
    director->runWithScene(scene);

    return true;
}

这里我们修改和添加的东西有:

glview = GLView::createWithRect("PlayerThePlane", Rect(0, 0, 480, 800));  我们设置了我们飞机的名字,和容纳的空间

glview->setDesignResolutionSize(480, 800, kResolutionNoBorder); 

注释很清楚,就不再解释了。

auto scene = HelloWorld::createScene();这个就是我们的开始场景,auto是c++11的特性。触景生情,好的场景,会让人情不自禁的想把这个飞机打下去,所以我们有必要要让场景炫起来。

director->runWithScene(scene);把scene场景交给导演来运作


三、游戏开始界面的具体实现

我们先看下HelloWorldScene.h代码:

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::Layer
{
public:
	// 产生一个场景,然后把本Layer层类加入到场景中去
    static cocos2d::Scene* createScene();

    // 在Layer层中添加精灵元素
    virtual bool init();  
    
    // a selector callback 退出按钮回调
    void menuCloseCallback(cocos2d::Ref* pSender);
    
	// 它的具体实现其实就是HelloWorld::create(),你进入CREATE_FUNC宏定义可以看到
    CREATE_FUNC(HelloWorld);

public:
	void loadingDone(Node* pNode); // 从开始界面 跳到游戏界面
	void PreloadMusicAndPicture(); // 预加载音乐和图片

};

#endif // __HELLOWORLD_SCENE_H__


好吧,我发现我都注释了,没什么好说,直接看HelloWorldScene.cpp代码:
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
	// 创建一个自动释放的场景
    auto scene = Scene::create(); 

	// 创建一个自动释放的layer层
    auto layer = HelloWorld::create();

	// 场景中加入layer层
    scene->addChild(layer);

    // 返回场景
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
	// 当你想调用父类的virtual,又想有自己的实现的时候,就这么写
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Point origin = Director::getInstance()->getVisibleOrigin();

	// 创建退出按钮
    auto closeItem = MenuItemImage::create(
                                           "CloseNormal.png",
                                           "CloseSelected.png",
                                           CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
    
	closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                                origin.y + closeItem->getContentSize().height/2));

    auto menu = Menu::create(closeItem, NULL);
    menu->setPosition(Point::ZERO);
    this->addChild(menu, 1);

    /////////////////////////////
    // 3. add your codes below...

    // add a label shows "Hello World"
    // create and initialize a label
    // 下面的代码去掉,加入自己的代码
	// 返回OpenGL视图的大小
	Size winSize=Director::getInstance()->getWinSize();

	// 预加载图片和音乐
	PreloadMusicAndPicture();

	// 背景图(精灵)
	auto background = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("background.png"));
	background->setPosition(Point(winSize.width/2,winSize.height/2)); // 设置位置

	// 场景中加入背景图
	this->addChild(background);

	// 加入copyright图片(精灵)
	auto copyRight = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("shoot_copyright.png"));
	copyRight->setAnchorPoint(Point(0.5, 0)); // 描点
	copyRight->setPosition(Point(winSize.width/2,winSize.height/2));
	this->addChild(copyRight);

	// 加入loading图片(精灵)
	auto loading = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("game_loading1.png"));
	loading->setPosition(Point(winSize.width/2,winSize.height/2));
	this->addChild(loading);

	// Animation是由许多精灵帧组成,可以设置间隔时间,持续时间等,它实际上是包含着一组数据
	Animation* animation=Animation::create();
	animation->setDelayPerUnit(0.2f); // 间隔时间
	animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("game_loading1.png"));
	animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("game_loading2.png"));
	animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("game_loading3.png"));
	animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("game_loading4.png"));

	// 通过帧数据创建帧动作(创建序列帧动画)
	Animate* animate=Animate::create(animation);
	Repeat* repeat=Repeat::create(animate,3); // 重复一个动作的次数 
	CallFuncN* repeatdone=CallFuncN::create(CC_CALLBACK_1(HelloWorld::loadingDone, this)); // 创建回调函数 CC_CALLBACK_1 代表一个参数
	Sequence* sequence=Sequence::create(repeat, repeatdone, NULL);// 让多个动作按照前后顺序逐一执行 repeatdone 放在 repeat前的话,就不会播放执行3次序列帧的动画
	loading->runAction(sequence); // 执行上述动画

	this->setKeypadEnabled(true); // 设置监听Android的按键,如返回键、菜单键、Home键等。
    return true;
}


void HelloWorld::menuCloseCallback(Ref* pSender)
{
    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

void HelloWorld::PreloadMusicAndPicture()
{
	//png加入全局cache中 plist存储了
	SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ui/shoot_background.plist");
	SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ui/shoot.plist");

	// 音效
	CocosDenshion::SimpleAudioEngine::getInstance()->preloadBackgroundMusic("sound/background-music1.mp3");
	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/bullet.mp3");
	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/enemy1_down.mp3");
	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/enemy2_down.mp3");
	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/enemy3_down.mp3");
	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/game_over.mp3");
	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/get_bomb.mp3");
	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/get_double_laser.mp3");
	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/use_bomb.mp3");
	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/big_spaceship_flying.mp3");
	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/achievement.mp3");
	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/out_porp.mp3");
	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/button.mp3");

	// 背景音乐
	CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("sound/game_music.mp3",true);
}

void HelloWorld::loadingDone( Node* pNode )
{

}


路径

预加载的路径是项目路径下的Resources文件夹

这个是我的工程资源路径:E:\studyCocos2dx3.0RC1\PlayThePlane\Resources

如:SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ui/shoot_background.plist");

其实就是SpriteFrameCache::getInstance()->addSpriteFramesWithFile("E:/studyCocos2dx3.0RC1/PlayThePlane/Resources/ui/shoot_background.plist");


图片加载

我们的图片是用TexturePacker工具把若干图片打包生成的一张总的png和plist,plist保存着png图片中的各个数据,比如名字大小什么的。当然你也可以不用这种整合的,那么加载图片的方式就改变了,比如背景图的加载:

	// 背景图(精灵)
	auto background = Sprite::create("ui/shoot_background/background.png");
	background->setPosition(Point(winSize.width/2,winSize.height/2)); // 设置位置

	// 场景中加入背景图
	this->addChild(background);


音乐加载

预加载中,有一个不是预加载,而是直接加载开启的:
CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("sound/game_music.mp3",true);这个是直接把开启了背景音乐。


图片动画效果以及游戏开始的回调

	CallFuncN* repeatdone=CallFuncN::create(CC_CALLBACK_1(HelloWorld::loadingDone, this)); // 创建回调函数 CC_CALLBACK_1 代表一个参数
	Sequence* sequence=Sequence::create(repeat, repeatdone, NULL);// 让多个动作按照前后顺序逐一执行 repeatdone 放在 repeat前的话,就不会播放执行3次序列帧的动画
	loading->runAction(sequence); // 执行上述动画
Sequence* sequence=Sequence::create(repeat, repeatdone, NULL);我的理解是,Sequence存放动作队列。其中repeat, repeatdone, NULL这个三个动作是顺序执行的,也就是说先执行完repeat动作(小飞机飞三次),然后执行repeatdone,从而触发回调函数loadingDone(),游戏的开始就是在这里哟。

好了,到这里就完成了所谓的游戏开始前的界面。下次说什么我也不知道,写什么,说什么吧。


我看了下我的排版,着实奇怪,有时候行和行间距离很近,有时候很远。而且怎么设置字体啊,我想一开始就是小型字体,而不是每次写完一段,再手动去改。

大家有什么不懂得,可以直接问我(不要问的太深入~),我也是刚开始学cocos2dx,大家一起学习。

성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
MySQL에서 뷰를 사용하는 한계는 무엇입니까?MySQL에서 뷰를 사용하는 한계는 무엇입니까?May 14, 2025 am 12:10 AM

mysqlviewshavelimitations : 1) 그들은 upportallsqloperations, datamanipulation throughviewswithjoinsorbqueries를 제한하지 않습니다

MySQL 데이터베이스 확보 : 사용자 추가 및 권한 부여MySQL 데이터베이스 확보 : 사용자 추가 및 권한 부여May 14, 2025 am 12:09 AM

적절한 usermanagementInmysqliscrucialforenhancingsecurityandensuringfefficientDatabaseOperation.1) USECREATEUSERTOWDDUSERS,@'localHost'or@'%'.

MySQL에서 사용할 수있는 트리거 수에 영향을 미치는 요인은 무엇입니까?MySQL에서 사용할 수있는 트리거 수에 영향을 미치는 요인은 무엇입니까?May 14, 2025 am 12:08 AM

mysqldoes notimposeahardlimitontriggers, butpracticalfactorsdeteirefectiveuse : 1) ServerConfigurationimpactStriggerManagement; 2) 복잡한 트리거 스케일 스케일 사이드로드; 3) argertableSlowtriggerTriggerPerformance; 4) High ConconcercencyCancaUspriggerContention; 5) m

MySQL : Blob을 저장하는 것이 안전합니까?MySQL : Blob을 저장하는 것이 안전합니까?May 14, 2025 am 12:07 AM

예, It 'safetostoreBlobdatainmysql, butconsidertheStefactors : 1) StoragesPace : BlobScanconSumeSignificantspace, 잠재적으로 증가하는 CostsandSlownperformance

MySQL : PHP 웹 인터페이스를 통해 사용자 추가MySQL : PHP 웹 인터페이스를 통해 사용자 추가May 14, 2025 am 12:04 AM

PHP 웹 인터페이스를 통해 MySQL 사용자를 추가하면 MySQLI 확장 기능을 사용할 수 있습니다. 단계는 다음과 같습니다. 1. MySQL 데이터베이스에 연결하고 MySQLI 확장자를 사용하십시오. 2. 사용자를 생성하고 CreateUser 문을 사용하고 Password () 함수를 사용하여 암호를 암호화하십시오. 3. SQL 주입 방지 및 MySQLI_REAL_ESCAPE_STRING () 함수를 사용하여 사용자 입력을 처리하십시오. 4. 새 사용자에게 권한을 할당하고 보조금 명세서를 사용하십시오.

MySQL : Blob 및 기타없는 SQL 스토리지, 차이점은 무엇입니까?MySQL : Blob 및 기타없는 SQL 스토리지, 차이점은 무엇입니까?May 13, 2025 am 12:14 AM

mysql'sblobissuilableforstoringbinarydatawithinareldatabase, whilenosqloptionslikemongodb, redis, and cassandraofferflexible, scalablesolutionsforunstuctureddata.blobissimplerbutcanslowwownperformance를 사용하는 것들보업 betterscal randaysand

MySQL 추가 사용자 : 구문, 옵션 및 보안 모범 사례MySQL 추가 사용자 : 구문, 옵션 및 보안 모범 사례May 13, 2025 am 12:12 AM

TOADDAUSERINMYSQL, 사용 : CreateUser'UserName '@'host'IdentifiedBy'Password '; 여기서'showTodoitseciRely : 1) ChoosetheHostCareLyTocon trolaccess.2) setResourcelimitswithOptionslikemax_queries_per_hour.3) Usestrong, iriquepasswords.4) enforcessl/tlsconnectionswith

MySQL : 문자열 데이터 유형을 피하는 방법 일반적인 실수?MySQL : 문자열 데이터 유형을 피하는 방법 일반적인 실수?May 13, 2025 am 12:09 AM

toavoidcommonmistakeswithstringdatatypesinmysql, stroundStringTypenuances, chooseTherightType, andManageEncodingAndCollationSettingSefectively.1) usecharforfixed-lengthstrings, varcharvariable-length, andtext/blobforlargerdata.2) setcarcatter

See all articles

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

Video Face Swap

Video Face Swap

완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

뜨거운 도구

Atom Editor Mac 버전 다운로드

Atom Editor Mac 버전 다운로드

가장 인기 있는 오픈 소스 편집기

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

PhpStorm 맥 버전

PhpStorm 맥 버전

최신(2018.2.1) 전문 PHP 통합 개발 도구