用Quick-Cocos2d-x 3.3简单开发微信打飞机 -01

前端之家收集整理的这篇文章主要介绍了用Quick-Cocos2d-x 3.3简单开发微信打飞机 -01前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

前言
楼主也是初学quick,想写几个简单的小项目入门,想想最简单的还是“微信打飞机”了(之前学Cocos2d-JS的时候也是从这个项目开始入手的)。


正文

这里只是写一个微信打飞机的Demo,所以,不需要有太多的功能。先简单地分析下:

大概分为四个部分来实现:

1.先新建项目实现一个飞机在屏幕中移动

自定义场景,

精灵的定义

屏幕点击事件

2.在游戏中添加敌人和子弹

数组,

计时器

3.添加碰撞检测

数组中元素的删除

动画

分数的实现

4.游戏结算

界面的切换,

这里先实现第一部分:

写一个MainScene和一个GameScen,MainScene作为菜单界面,点击菜单界面进入GameScene(游戏主界面),这里需要用到精灵的创建,触屏事件的添加,界面的切换。

下面上代码

MainScene:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
localMainScene= class ( "MainScene" ,function()
return display.newScene( )
end)
functionMainScene:ctor()
--添加一个背景精灵
self.bgSprite=display.newSprite( "game_bg.jpg" ):addTo(self)
self.bgSprite:pos(display.cx,display.cy)
--添加一个TouchLayer
self.touchLayer=display.newColorLayer(cc.c4b(255,255,0)):addTo(self)
self.touchLayer:setTouchEnabled( true )
self.touchLayer:addNodeEventListener(cc.NODE_TOUCH_EVENT,function(event)
if event.name== "ended" then
--切换界面
localnextScene=require( "src.app.scenes.GameScene" ). new ()
display.replaceScene(nextScene, "fade" end
return true
end)
end
functionMainScene:onEnter()
end
functionMainScene:onExit()
end
MainScene

GameScene代码

29
30
31
32
33
34
35
36
37
38
39
40
41
42
localGameScene= "GameScene" functionGameScene:ctor()
--添加Player精灵
self.player=display.newSprite( "hero_1.png" self.player:pos(display.cx,monospace!important; font-size:1em!important; min-height:inherit!important; background:none!important">self.player:setLocalZOrder(10)
self.touchPos=cc.p(0,0)
--添加一个TouchLayer接收点击事件
)
print(event.name)
"began" then
self.touchPos=cc.p(event.x,event.y)
elseifevent.name== "moved" then
localx=event.x-self.touchPos.x;
localy=event.y-self.touchPos.y;
self:onTouch(x,y);
else
true
end)
end
functionGameScene:onTouch(x,y)
--更新飞机的位置,没有对飞机是否会触屏做限制
self.player:pos(self.player:getPositionX()+x,self.player:getPositionY()+y)
end
functionGameScene:onEnter()
end
functionGameScene:onExit()
end
GameScene


感谢本文笔者(最后的牛仔)的分享,Cocos引擎中文官网欢迎更多的开发者分享开发经验。来稿请发送至:support@cocos.org

原文链接:https://www.f2er.com/cocos2dx/343319.html

猜你在找的Cocos2d-x相关文章