Home Database Mysql Tutorial 【一】仿微信飞机大战cocos2d

【一】仿微信飞机大战cocos2d

Jun 07, 2016 pm 03:42 PM
airplane

参考 【偶尔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程序正式开始运行
}
Copy after login
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;
}
Copy after login

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

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__
Copy after login


好吧,我发现我都注释了,没什么好说,直接看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 )
{

}
Copy after login


路径

预加载的路径是项目路径下的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);
Copy after login


音乐加载

预加载中,有一个不是预加载,而是直接加载开启的:
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); // 执行上述动画
Copy after login
Sequence* sequence=Sequence::create(repeat, repeatdone, NULL);我的理解是,Sequence存放动作队列。其中repeat, repeatdone, NULL这个三个动作是顺序执行的,也就是说先执行完repeat动作(小飞机飞三次),然后执行repeatdone,从而触发回调函数loadingDone(),游戏的开始就是在这里哟。

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


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

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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Juneyao Airlines' 'Genshin Impact” has officially started sailing. During the first flight week, enjoy the special experience of commemorating boarding passes, lunch boxes and water bottles. Juneyao Airlines' 'Genshin Impact” has officially started sailing. During the first flight week, enjoy the special experience of commemorating boarding passes, lunch boxes and water bottles. Nov 11, 2023 pm 06:37 PM

On November 11, Juneyao Airlines announced that “Genshin Impact” had successfully made its maiden flight, and also launched Juneyao Airlines × Genshin Impact-themed peripheral products. These peripheral products include a 787 aircraft model made at a scale of 1:150, a "Genshin Impact" third-anniversary cooperation passport bag, and a third-anniversary cooperation flight blanket. Our website noted that the "Genshin Impact" will fly in 2023. From November 8 to November 8, 2024, the aircraft type is Boeing 787-9 wide-body aircraft, with 29 business class seats and 295 economy class seats. How to purchase tickets: Open the Juneyao Airlines App and click on the top picture of "Genshin Impact 3rd Anniversary" on the homepage. Go to the "Genshin Impact 3rd Anniversary Themed Flight" landing page to view the planned "Genshin Impact" flights. Select the departure time and click "Book" Enter the ticket purchase page for the first flight

Airlines adjust rules for ticket refunds and changes, Air China provides domestic no-reason refund coupons x 2 Airlines adjust rules for ticket refunds and changes, Air China provides domestic no-reason refund coupons x 2 Jan 08, 2024 pm 06:26 PM

According to news from this site on January 6, China Southern Airlines and China Eastern Airlines have successively issued announcements on adjustments to their ticket cancellation and change rules, including factors such as aircraft model changes that lead to inability to travel, flight changes that lead to route changes, and flight stopover delays, etc., into the application for free ticket refunds and changes. scope, and at the same time relax the rules for cancellation due to illness, which will take effect from 0:00 on January 10, 2024, Beijing time. China Southern Airlines has made the following adjustments: Expanding the scope of free ticket refunds and changes to include factors such as changes in aircraft types that prevent you from making the trip, flight stoppage delays, airport transfers, force majeure and other factors. Relaxation of Cancellation and Refund Rules Due to Sickness 1. Passengers who are unable to travel due to illness can apply for changes or refunds due to illness for domestic and international tickets held by China Southern Airlines. 2. The proof of cancellation due to illness is adjusted to payment invoice or hospitalization deposit certificate (choose one of the two)

Southwest Airlines is expected to obtain certification for the Boeing 737 MAX 7 aircraft in April next year and plans to put it into operation in October-November. Southwest Airlines is expected to obtain certification for the Boeing 737 MAX 7 aircraft in April next year and plans to put it into operation in October-November. Nov 11, 2023 am 09:37 AM

According to news from this website on November 11, according to Reuters, a Southwest Airlines executive revealed that the Boeing 737 MAX7 aircraft will be certified by the US Federal Aviation Administration in April 2024 and will begin operations from October to November. This site noticed that Southwest Airlines announced new orders for an additional 108 MAX7 aircraft last month, which are expected to be delivered before 2031. However, the aircraft's delivery schedule depends on certification by the US Federal Aviation Administration (FAA), and delays in certification have forced it to convert dozens of MAX7 orders to MAX8. On Thursday night, Southwest Airlines Chief Operating Officer Andrew Watterson said in an interview: "Our internal planning is that we expect it to be in 4

AG100 'Lingyan', a junior civilian training aircraft independently developed in China, was officially launched and delivered. The equipment is equipped with automatic driving technology. AG100 'Lingyan', a junior civilian training aircraft independently developed in China, was officially launched and delivered. The equipment is equipped with automatic driving technology. Jan 23, 2024 am 08:33 AM

This website reported on January 21 that Aviation Industry General Aviation held a delivery ceremony for the first batch of new-generation domestic junior trainer aircraft "Lingyan" AG100. The first batch was delivered to Henan New Silk Road Aviation School and Zhuhai AVIC Flight School. This website noticed that the "Lingyan" AG100 aircraft is a new generation of domestically produced civilian primary trainer aircraft independently developed by Aviation Industry General Aviation. It has introduced a third-seat auxiliary training function to improve the efficiency of flight training; it is equipped with an automatic driving system and a parachute landing system. system to ensure the safety of the crew and passengers to the greatest extent; it adopts high-reliability design, and the entire aircraft adopts low-cost composite material design and manufacturing technology. At the same time, the AG100 is equipped with the latest ROTAX915is engine, which can use automotive gasoline to facilitate fuel security and reduce training costs.

Airbus' first full life cycle aircraft service center launched in Chengdu, Sichuan, China Airbus' first full life cycle aircraft service center launched in Chengdu, Sichuan, China Jan 24, 2024 pm 11:33 PM

Airbus Aircraft Full Life Cycle Service Center announced today that it is put into operation and will provide solutions for aircraft full life cycle management. The center is located in Chengdu, Sichuan, China. According to reports, as its first one-stop service center, the Airbus aircraft full life cycle service center will be oriented to various aircraft models, covering business scope from aircraft parking and storage to maintenance, upgrades, modifications, dismantling, recycling and disassembly. Dissolved second-hand aviation supplies distribution business. The service center has obtained relevant certifications from the European Aviation Safety Agency (EASA) and the Civil Aviation Administration of China. It is planned to cover a total area of ​​717,000 square meters in the future and is planned to build and park 125 aircraft. From operation to 2025, the center will gradually expand operations and directly employ 150 employees. Airbus aircraft full life cycle services

Detailed steps to add flying dynamic effects to airplanes in PPT Detailed steps to add flying dynamic effects to airplanes in PPT Mar 26, 2024 pm 09:01 PM

1. First download an airplane picture material, it is recommended to download it in PNG format; after downloading, create a new PPT file, drag the material into the PPT, hold down the shift key, reduce the size of the airplane proportionally, and move it to the right of the PPT lower corner. 2. Now you need to add animation to it: select the airplane picture, click [Animation]-[Action Path]-[Straight Line], and a straight line will automatically appear. Do you see it? . 3. Now this path is not the path we want. We need to adjust the position of the straight line so that the plane flies from the lower left corner to the upper right corner. After selecting the red dot, the target path of the plane will appear. The color is much lighter. Move it. Go to the upper right corner and the path will be changed. Now try the playback effect. 4. After clicking the preview, I found something was wrong. The plane was flying.

Boeing: The world will need nearly 44,000 new commercial aircraft in the next 20 years Boeing: The world will need nearly 44,000 new commercial aircraft in the next 20 years Jul 23, 2024 pm 06:55 PM

This website reported on July 22 that on the eve of the Farnborough Air Show, Boeing released the 2024 Civil Aviation Market Outlook (CMO), predicting that global airlines will need nearly 44,000 new civil aircraft by 2043. With air travel fully restored, the latest aircraft deliveries over the next 20 years are up 3% from last year's forecast. The outlook also predicts that emerging market and global single-aisle market demand will continue to be the main growth driver for the civil aviation industry. According to the CMO, air passenger traffic will grow at an average annual rate of 4.7% over the next 20 years compared with 2023. Highlights of the CMO forecast for the next 20 years include: The global civil fleet is expected to grow by 3.2% per year, a slower growth rate than air traffic as airlines continue to increase load factors and increase the number of aircraft per day.

How to use C++ to implement a simple airplane booking system? How to use C++ to implement a simple airplane booking system? Nov 02, 2023 pm 12:35 PM

How to use C++ to implement a simple airplane booking system? As air traffic develops and people's demand for comfortable travel increases, aircraft booking systems have become increasingly important. In this article, we will learn how to implement a simple airplane booking system using the C++ programming language. This system will allow users to check flight information, select seats, book and cancel tickets and other functions. First, we need to create a Flight class to represent flights. This class should include the following attributes: flight number, departure place, destination, departure time

See all articles