您的位置:首页 > 移动开发 > Cocos引擎

Cocos2d-x 3.2 lua飞机大战开发实例(一)背景地图,飞机,子弹的封装,以及lua中定时器的使用

2014-12-20 17:08 961 查看

Lua飞机大战开发实例(一)

背景地图,飞机,子弹的封装,以及lua中定时器的使用

1.   首先实现场景的跳转

在MenuScene场景中

 
require "Cocos2d"
 
--创建一个菜单场景
local MenuScene=class("MenuScene",
function ()
return cc.Scene:create()
end
)
--添加一个creaet函数
function MenuScene:create()
local scene=MenuScene.new()
scene:addChild(scene:init())
return scene
end
 
--添加一个构造
function MenuScene:ctor()
    self.winsize=cc.Director:getInstance():getWinSize()
end
--初始化
function MenuScene:init()
--定义一个layer作为容器
local layer=cc.Layer:create()
 
--添加一个精灵背景
local spbk=cc.Sprite:create("fonts/Resources/logo.jpg")
layer:addChild(spbk)
spbk:setPosition(cc.vertex2F(self.winsize.width/2,self.winsize.height/2))
--定义logo
local splogo=cc.Sprite:create("fonts/Resources/LOGO.png")
layer:addChild(splogo)
--执行动作,从底下移动到上方
splogo:setPositionX(self.winsize.width/2)
splogo:runAction(cc.MoveTo:create(0.5,cc.vertex2F(self.winsize.width/2,self.winsize.height/2+300)))
--添加一个按钮
--local label1=cc.LabelTTF:create("开始游戏","",23)
local label1=cc.MenuItemImage:create("fonts/Resources/kaishia.png","fonts/Resources/kaishib.png")
local start=cc.MenuItemLabel:create(label1)
local menu=cc.Menu:create()
menu:addChild(start)
start:setTag(10)
start:setColor(cc.c4f(255,255,1,1))
layer:addChild(menu)
 
    --locallabel2=cc.LabelTTF:create("游戏帮助","",23)
    local label2=cc.MenuItemImage:create("fonts/Resources/xuanyaoa.png","fonts/Resources/xuanyaob.png")
    local help=cc.MenuItemLabel:create(label2)
    local menu=cc.Menu:create()
    menu:addChild(help)
    help:setPositionY(help:getPositionY()-100)
    help:setTag(11)
    help:setColor(cc.c4f(255,255,1,1))
    layer:addChild(menu)
--添加回调函数
   local function menucallback(obj)
  
       if tonumber(obj)==10 then
      local scene=require("GameScene")
       local gs=scene:create()
       cc.Director:getInstance():replaceScene(gs)
       elseif tonumber(obj)==11 then
       local scene=require("HelpScene")
       local hs=scene:create()
       cc.Director:getInstance():replaceScene(hs)
    
 end
   end
help:registerScriptTapHandler(menucallback)
    start:registerScriptTapHandler(menucallback)
    return layer
end
 return MenuScene
 

2.  在HelpScene的场景中:

--

require("Cocos2d")
 
--创建一个场景
local HelpScene=class("HelpScene",
function ()
   return cc.Scene:create()
end)
 
--添加一个create函数
function HelpScene.create()
  local scene=HelpScene.new()
  scene:addChild(scene:init())
  return scene
end
 
--添加一个构造函数
function HelpScene:ctor()
self.winsize=cc.Director:getInstance():getWinSize()
  
end
 
 
function HelpScene:init()
--添加容器
local layer=cc.Layer:create()
--添加背景
local spbk=cc.Sprite:create("fonts/Resources/gy.jpg")
layer:addChild(spbk)
spbk:setPosition(self.winsize.width/2,self.winsize.height/2)
 
--添加帮助信息还可以这样写
--local Texthelp=ccui.Text:create("这里是帮助,通过控制飞机躲避子弹,祝你成功!")
--layer:addChild(Texthelp)
--Texthelp:setColor(cc.c4f(0,0,255,255))
--Texthelp:setContentSize(300,400)--设置文本区域大小
--Texthelp:ignoreContentAdaptWithSize(false)--忽略字体的大小在文本区域为假
--Texthelp:setPosition(cc.vertex2F(self.winsize.width/2,self.winsize.height/2))
 
--单纯的添加文字
local label=cc.LabelTTF:create("这里是帮助","",23)
label:setPosition(self.winsize.width/2,self.winsize.height/2)
label:setScale(2)
label:setColor(cc.c4f(0,255,255,255))
layer:addChild(label)
  
  
--按下时添加回调函数
local function touchBegan(event,touch)
    local scene=require("MenuScene")
    local ms=scene:create()
    local tms=cc.TransitionFade:create(0.5,ms)
    cc.Director:getInstance():replaceScene(tms)
    
end
   local listener=cc.EventListenerTouchOneByOne:create()
   listener:registerScriptHandler(touchBegan,cc.Handler.EVENT_TOUCH_BEGAN)
-—在lua的开发中,回调函数,是与c++不一样的
cc.Director:getInstance():getEventDispatcher():addEventListenerWithSceneGraphPriority(listener,layer)
  
  
   return layer
  
  end
 
 
return HelpScene

 

 

3.  在GameScene中:

--

require "Cocos2d"
local GameScene = class("GameScene",function()
    return cc.Scene:create()
end)
 
function GameScene.create()
    local scene = GameScene.new()
    scene:addChild(scene:init())
  
    return scene
end
 
 
function GameScene:ctor()
    self.winsize = cc.Director:getInstance():getWinSize()
end
 
--初始化
function GameScene:init()
--定义容器
local layer=cc.Layer:create()
 
--添加背景
--添加飞机
--添加子弹---定时器

--添加敌机

--添加屏幕的触摸处理

 

 

 

return layer

end

return GameScene

--这样我们就实现了场景的基本跳转

4.接下来我们就实现背景地图的封装

首先为了我们清晰直观的看懂,把封装的node独立一个分组,nodes:

 

创建一个BackGround的类文件

--把背景类作为一个封装
require "Cocos2d"
 
local BackGround=class("BackGround",
function ()
   return cc.Node:create()
end
)
 
--创建一个create函数
function BackGround:create()
   local bgnode=BackGround.new()
   bgnode:addChild(bgnode:init())
   return bgnode
  
end
--创建一个构造的函数
function BackGround:ctor()
   self.by=0--当前背景y的坐标
   self.winsize=cc.Director:getInstance():getWinSize()
end
--定义初始化的函数
function BackGround:init()
local layer=cc.Layer:create()
--添加背景精灵—两张
local spbg=cc.Sprite:create("fonts/Resources/gm.jpg")
layer:addChild(spbg)
spbg:setAnchorPoint(0,0)—锚点
spbg:setPositionX(0)
spbg:setPositionY(0)
    local spbg2=cc.Sprite:create("fonts/Resources/gm.jpg")
    layer:addChild(spbg2)
    spbg2:setAnchorPoint(0,0)
    spbg2:setPositionX(0)
    spbg2:setPositionY(0)
 
 
--时间计划任务
local function logic(t)
   spbg:setPositionY(spbg:getPositionY()-1)
   spbg2:setPositionY(spbg:getPositionY()+1136)--设置第二张的图片永远在第一张的上边
   if spbg:getPositionY()<-1136 then
     spbg:setPositionY(0)
    
   end
end
--时间帧循环---0是计划任务的优先级---在一个节点上计划任务
layer:scheduleUpdateWithPriorityLua(logic,0)
 
 
return layer
end
return BackGround

4.我们定义好了地图的封装

在GameScene的init中,我们添加背景

 

--添加背景
self:addBackGround(layer) —-把背景添加到这个容器中
--首先做一个函数

function GameScene:addBackGround(nowlayer)--把layer传过来,这样我们就都添加到layer中
   --创建一个背景
   local back=require("nodes.BackGround")
   local bg=back:create()
   nowlayer:addChild(bg)
end
 

 

这样地图就可以自动的循环转动了

 

 

5实现飞机的封装  Plane

--我们首先定义一个全局的数据类

GameData.Lua

--全局当前游戏数据
g_px=0  --当前的飞机位置横坐标
g_py=0  --当前的飞机位置纵坐标
allBullet={}
allEnemy={}

 
 
 
 
--飞机类的封装
require "Cocos2d"
require "GameData"—在这里我们引入了GameData。这里是定义了一些全局的变量,
local Plane=class("Plane",
function ()
return cc.Node:create()
  
end)
--create函数
function Plane:create(x,y)
   local p=Plane.new()
   p:addChild(p:init(x,y))
   return p
end
 
--构造
function Plane:ctor()
self.winsize=cc.Director:getInstance():getWinSize()
--定义飞机精灵为空‘
self.sp=nil--为了在飞机移动的方法中可以调用飞机的精灵
end
--初始化
function Plane:init(x,y)
   local layer=cc.Layer:create()
   --添加飞机的动画--创建动画的每一帧
   local allf={}
   for i=1 ,4  do
   allf[i]=cc.SpriteFrame:create("fonts/Resources/npc1.png",cc.rect((i-1)*32,0,32,48))
   end
   --创建动画
   local animation=cc.Animation:createWithSpriteFrames(allf,0.1)
   local animate=cc.Animate:create(animation)
   --创建精灵绑定动画
   local sp=cc.Sprite:create()
   self.sp=sp
   sp:runAction(cc.RepeatForever:create(animate))
   layer:addChild(sp)
   sp:setPosition(x,y)
   sp:setScale(4)
 
 return layer
end
--飞机移动的方法
function Plane:moveTo()
  self.sp:setPosition(g_px,g_py)
end
 

return Plane

 

6.我们定义好了飞机的封装

在GameScene的init中,我们添加飞机

--添加飞机
self:addPlane(layer)—添加到layer中

--同样先定义一个添加飞机的函数
function GameScene:addPlane(nowlayer)
    --创建一个飞机
    local plane=require("nodes.Plane")
    --这里记得引入require “GameData”,因为需要使用里边的全局数据g_px,g-py
    g_px=self.winsize.width/2--
    g_py=100
   
    local pl=plane:create(g_px,g_py)
    pl:setTag(100)
    nowlayer:addChild(pl)
end
--7.这样飞机就添加进来了,但是是现在我们需要移动它,所以就需要添加触摸的处理,与c++中思路一样,我们这样做,
 
 
--在构造中还得需要先定义初始点的横纵坐标
    self.touchX=0--初始点横坐标
self.touchY=0--初始点纵坐标
 
在init()中
--添加屏幕的触摸处理—touchBegan-touchMoved-touchEnded
    local function touchBegan(touch,event)
    self.touchX=touch:getLocation().x
    self.touchY=touch:getLocation().y
  
  
    return true
    end
 
    local function touchMoved(touch,event)
     
       local mx=touch:getLocation().x-self.touchX
       local my=touch:getLocation().y-self.touchY
       --屏幕限制—条件判定
       if g_px+mx<0 or g_px+mx>self.winsize.width then
          return
       end
       if g_py+my<0 or g_py+my>self.winsize.height then
          return
       end
       --飞机移动的相对距离
       g_px=g_px+mx
       g_py=g_py+my
      
       layer:getChildByTag(100):moveTo()--刷新飞机的坐标
       self.touchX=touch:getLocation().x
       self.touchY=touch:getLocation().y
      
    end
 
local function touchEnded(touch,event)
 end
    --注册侦听
    local listener=cc.EventListenerTouchOneByOne:create()
    listener:registerScriptHandler(touchBegan,cc.Handler.EVENT_TOUCH_BEGAN)
    listener:registerScriptHandler(touchMoved,cc.Handler.EVENT_TOUCH_MOVED)
    listener:registerScriptHandler(touchEnded,cc.Handler.EVENT_TOUCH_ENDED)
cc.Director:getInstance():getEventDispatcher():addEventListenerWithSceneGraphPriority(listener,layer)
 
这样就可以实现飞机的触摸和移动的处理了,

8.做好飞机的封装和移动,接下来就做子弹的封装,

 

 

定义一个子弹的封装类

--子弹的封装
require "Cocos2d"
local Bullet=class("Bullet",
function ()
   return cc.Node:create()
end)
 
--创建一个create
function Bullet :create(x,y)
local bullet =Bullet.new()
bullet:addChild(bullet:init(x,y))
return bullet
end
 
--构造
function Bullet:ctor()
self.bx=0
self.by=0
self.type=0
self.sp=nil --子弹的精灵图片
self.winsize=cc.Director:getInstance():getWinSize()
end
 
--init
function Bullet:init(x,y)
   local  layer=cc.Layer:create()
   local sp=cc.Sprite:create("fonts/Resources/bullet0.png")
   self.sp=sp       --让子弹的精灵图片指向空得sp
   layer:addChild(sp)
   sp:setPosition(x,y)
   self.bx=x
   self.by=y
   --创建好了子弹,我就移动当前这个子弹
  
    local function moveTo(t)
      self.by=self.by+10
      sp:setPositionY(self.by)
      if self.by>self.winsize.height then
      self:removeFromParent()
      end
     end
  
   layer:scheduleUpdateWithPriorityLua(moveTo,0)
  
   return layer
end
return Bullet

9.封装好了子弹,接下来就在GameScene中产生子弹

--首先需要在构造函数中添加一个变量,用于做判定

self.countBullet=0    --相当于lua中的定时器,用于整数做判定

 

--在GameScene的init()中我们添加子弹的函数

--添加子弹---定时器
local function newBullet(t)
self.countBullet=self.countBullet+1--如果每秒钟执行20帧,
if self.countBullet>=20 then
   --产生子弹
   local bullet=require ("nodes.Bullet")
   local newb=bullet:create(g_px,g_py+30)
   layer:addChild(newb)
   self.countBullet=0
  
end
 
end
--这样执行
local node=cc.Node:create()
layer:addChild(node)
node:scheduleUpdateWithPriorityLua(newBullet,0)

--到这里我们就实现了游戏的背景地图,飞机,子弹的封装,,地图可以的自动滚动,飞机可以移动,子弹可以定时的产生,和发射,下节课将会讲敌机的封装和碰撞检测
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐