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

这次将实现子弹的配置文件,敌人的配置文件,子弹的精灵和敌人的精灵。以及子弹的发射和敌人的产生。

子弹的分析:

游戏中不可能只有一种子弹,这样子弹就需要一个配置文件配置文件中有子弹的类型,子弹的贴图,子弹的生命以及子弹的移动速度。

敌人的分析:

同样,游戏中会出现多种敌人,所以,也需要一个敌人的配置文件,其中包括敌人的类型,贴图,速度,生命等属性

在这里新建了一个目录(data)来存放配置文件

data中有两个配置文件一个是子弹的配置文件一个是敌人的配置文件

BulletConfig.lua源码

1
2
3
4
5
6
7
8
9
10
11
12
13
config_bullets={}
config_bullets[1]={
id=1,
skin= "bullet_0.png" ,
life=10,
speed=0.5
}
config_bullets[2]={
id=2,
"bullet_1.png" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
speed=0.2
}

EnermyConfig.lua源码

13
14
15
16
17
18
19
20
config_enermy={}
config_enermy[1]={
"enemy_b.png" speed=0.2,
life=100
}
config_enermy[2]={
"enemy_m.png" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
life=50
}
config_enermy[3]={
id=3,
"enemy_s.png" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
life=10
一个配置文件对应一个精灵,即,子弹的配置文件对应的是子弹的精灵,敌人的配置文件对应的是敌人的精灵。

EnermySprite.lua源码:

20
21
22
23
24
25
26
27
28
29
30
localscheduler=require(cc.PACKAGE_NAME.. ".scheduler" )
localBulletSprite= class ( "BulletSprite" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,function(path)
return display.newSprite(path)
end)
functionBulletSprite:create(data)
localbulleSprite=BulletSprite. new (data.skin)
for k,vinpairs(data) do
bulleSprite[k]=v
end
bulleSprite:init()
bulleSprite
end
functionBulletSprite:ctor()
end
functionBulletSprite:init()
--子弹移动的计时器
scheduler.scheduleGlobal(function()
localspeed=(self.speed*display.height)/60
self:pos(self:getPositionX(),speed+self:getPositionY())
end,1.0/60)
end
BulletSprite

EnermySprite.lua源码

24
localEnermySprite= "EnermySprite" functionEnermySprite:create(data)
localenermy=EnermySprite. enermy[k]=v
enermy:init()
enermy
end
functionEnermySprite:init()
scheduler.scheduleGlobal(function()
localspeed=(self.speed*display.height)/60
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,self:getPositionY()-speed)
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,1.0/60)
end
EnermySprite

这里实现了子弹和敌人,接着将他们添加到游戏主界面中就OK了。

在这里,我将它们都放在了一个计时器中,当然,实际上他们是不应该在一个计时器中的。因为,游戏中产生敌人和子弹的节奏应该是不一致的。

GameScene的源码如下:

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
require( "src/app/scenes/Utils" )--这个是我的一个工具类,这里没有用到,所以,有没有这句话都行
"src/app/data/EnermyConfig" )
localBulletConfig=require( "src/app/data/BulletConfig" )
localBulletSprite=import( ".BulletSprite" )
localEnermySprite=import( ".EnermySprite" )
)
localGameScene= "GameScene" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,function()
display.newScene( )
end)
functionGameScene:ctor()
--添加Player精灵
self.player=display.newSprite( "hero_1.png" ):addTo(self)
self.player:pos(display.cx,display.cy)
self.player:setLocalZOrder(10)
self.touchPos=cc.p(0,0)
self.bullets={}
self.enermy={}
--添加一个TouchLayer接收点击事件
self.touchLayer=display.newColorLayer(cc.c4b(255,255)):addTo(self)
self.touchLayer:setTouchEnabled( true )
self.touchLayer:addNodeEventListener(cc.NODE_TOUCH_EVENT,function(event)
if 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;
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,event.y);
self:onTouch(x,y);
else
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,0)
end
return true
end)
--添加子弹的创建计时器
scheduler.scheduleGlobal(function()
self:createBullet()--这里将产生子弹和敌人放在了同一个计时器中,实际上不应该是这样的
self:createEnermy()
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,1)--1为时间
end
functionGameScene:onTouch(x,y)
--更新飞机的位置,没有对飞机是否会触屏做限制
self.player:pos(self.player:getPositionX()+x,self.player:getPositionY()+y)
end
functionGameScene:createBullet()
localconfig=config_bullets[1]
localbullet=BulletSprite:create(config):addTo(self)
bullet:pos(self.player:getPosition())
self.bullets[bullet]=bullet
end
functionGameScene:createEnermy()
--print( "createenermyschedule" )
localconfig=config_enermy[1]
localx=math.random(100,400)
localy=display.height+100
localenermy=EnermySprite:create(config):addTo(self)
enermy:setPosition(cc.p(x,y))
self.enermy[enermy]=enermy
end
functionGameScene:onEnter()
end
functionGameScene:onExit()
end
GameScene


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

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...