使用quick-cocos2dx-lua 实现的小游戏(包含碰撞检测,触屏发子弹)

--主界面

local MainScene = class("MainScene", function()
    return display.newScene("MainScene")
end)


ON = true;
function MainScene:ctor()
   local bg = cc.Sprite:create("main_background.png");
   bg:setScale(2);
   bg:setPosition(display.cx,display.cy);
   self:addChild(bg)


--冒险  图片菜单项
   local maoxian = cc.MenuItemImage:create("select10.png","select11.png");
   maoxian:setScale(2);
   maoxian:setPosition(690,550);
   local function menuMaoxian(psender)
    --print("called")
    local GameScene =  require("app.scenes.GameScene")
    local s = GameScene:new();
    cc.Director:getInstance():pushScene(s);
   end


   maoxian:registerScriptTapHandler(menuMaoxian);


 -- 添加“mini”图片菜单项
    local  mini  = cc.MenuItemImage:create("select20.png","select21.png");
    mini:setPosition(cc.p(680,450));
    mini:setScale(2);


    local function menuItemMiniCallback( sender )
    local GameScene =  require("app.scenes.GameScene")
    local s = GameScene:new();
    cc.Director:getInstance():replaceScene(s);
    print("mini")
    end
    mini:registerScriptTapHandler(menuItemMiniCallback);


    --"yizhi"模式的图片菜单项
    local   yizhi =  cc.MenuItemImage:create("select30.png","select31.png");
    yizhi:setScale(2);
    yizhi:setPosition(680,360);


    local function menuItemYizhiCallback( sender )
    local GameScene =  require("app.scenes.GameScene")
    local s = GameScene:new();
    cc.Director:getInstance():replaceScene(s);
    print("yizhi");
    end
    yizhi:registerScriptTapHandler(menuItemYizhiCallback);
 
    --生存 “shengcun”图片菜单项
    local shengcun = cc.MenuItemImage:create("select40.png","select41.png");
    shengcun:setScale(2);
    shengcun:setPosition(670,280);


    local function menuItemShengcunCallback( sender )
    local GameScene =  require("app.scenes.GameScene")
    local s = GameScene:new();
    cc.Director:getInstance():replaceScene(s);
    print("shengcun")
    end
    shengcun:registerScriptTapHandler(menuItemShengcunCallback);


   ON = true;
   


   local musicON =  cc.MenuItemFont:create("On");
   local musicOFF = cc.MenuItemFont:create("OFF");
   local toggle = cc.MenuItemToggle:create(musicON,musicOFF);
    


   toggle:registerScriptTapHandler(Music);
   
   toggle:setPosition(100,100);


   local mn = cc.Menu:create(maoxian,mini,yizhi,shengcun,toggle);
   mn:setPosition(0,0);
   self:addChild(mn);


    -- cc.ui.UILabel.new({
    --         UILabelType = 2, text = "Hello, World", size = 64})
    --     :align(display.CENTER, display.cx, display.cy)
    --     :addTo(self)


end
--实现音乐的播放 与暂停
  function Music(psender)
     if ON then
      audio.playMusic("backMusic.mp3", true)
      ON = false;
     elseif ON == false then
      audio.pauseMusic();
      ON = true;
     end
 
     end




function MainScene:onEnter()
end


function MainScene:onExit()
end


return MainScene

--游戏界面

local GameScene = class("GameScene",function (  )
return display.newScene();
end)


function GameScene:ctor()

     self.hero = cc.Sprite:create("hero.jpg");
    self.hero:setPosition(100,display.cy);
    self:addChild(self.hero);
   bulletTable = {}
   enemyTable = {}
   


 
self:touchDemo();
self:ScheduleDemo();
--碰撞检测  定时器
self:CollisionSchedule();


    


    
end
--碰撞检测定时器
function GameScene:CollisionSchedule( )
local schedule = cc.Director:getInstance():getScheduler();
local f = handler(self, self.Collision);
self.id = schedule:scheduleScriptFunc(f,1/60.0,false);

end
function GameScene:Collision()

--local bulletX,bulletY = self.hero:getPosition();--可以调用全局的变量的坐标
-- printf("*******%f", bulletX);
-- local enemyX,enemyY = self.enemy:getPosition();
--敌人和子弹的碰撞检测
if #bulletTable>0 then
for i,bn in pairs(bulletTable) do
local bx = bn:getPositionX();
local by = bn:getPositionY();
for k,en in pairs(enemyTable) do
local ex = en:getPositionX();
local ey = en:getPositionY();
if math.abs(bx-ex)<35 and math.abs(by-ey)<35 then
print("碰撞")
--子弹移除
                    table.remove(bulletTable,i)
                    bn:removeFromParent();


                    --僵尸移除
                    table.remove(enemyTable,k)
                    en:removeFromParent();
                    --fenshu


end
end
end
end
--敌人和英雄的碰撞检测


if #enemyTable >0 then
for k,en in pairs(enemyTable) do
local ex = en:getPositionX();
local ey = en:getPositionY();
if math.abs(ex-100)<100 and math.abs(ey-display.cy)<100 then
print("game over");
end
end
end
     





end








--调用定时器 添加敌人
function GameScene:ScheduleDemo(  )
local schedule = cc.Director:getInstance():getScheduler();
local f = handler(self, self.Enemy);


self.id = schedule:scheduleScriptFunc(f,1.0,false);
end








--添加敌人
function GameScene:Enemy()
print("Enemy");




local frames = display.newFrames("Zombatar_%d.jpg", 1, 14, true);
local animation = display.newAnimation(frames, 0.1);
local animate = cc.Animate:create(animation);


--设置敌人出来的位置
--math.randomseed(os.time())--???
self.enemy = display.newSprite("#Zombatar_1.jpg");--添加上self表示的是在别的函数中也是可以调用的
self.enemy:setScale(0.25);
    
local Realx = 1000;
local Realy  =math.random(30,610);
    self.enemy:setPosition(Realx,Realy);
    self:addChild(self.enemy);
    --添加到数组
    table.insert(enemyTable, self.enemy);
 


local   moveto = cc.MoveTo:create(5.0,cc.p(-50,Realy));
 
     local  span = cc.Spawn:create(animate,moveto);
     self.enemy:runAction(cc.RepeatForever:create(span));




--print("okokokokokokok")
end


function GameScene:touchDemo()


local  function onTouchBegan(touch, event)
local v = touch:getLocation();
print(v.x)
print(v.y)
-- local target = event:getCurrentTarget();
print("onTouchBegan")
return true;
end


 local function onTouchMoved(touch, event)
print("Moved")
end
--英雄发射子弹
local  function onTouchEnded(touch, event)


  local des = touch:getLocation();
   
 printf("(%f,%f)", des.x,des.y);
local  bullet = cc.Sprite:create("bullet.png");
 bullet:setPosition(0,display.cy);
--self.sprite1
self:addChild(bullet);


 
 table.insert(bulletTable, bullet)
 --子弹实际移动的x,y坐标
 local offX = des.x ;
 local offY = des.y -display.cy;


 if offX<=0 then
  return ;
 end


  local angle = offY/offX;
  local realX = display.width+bullet:getContentSize().width;
  local realY = realX*angle +display.cy;
  local realPos = cc.p(realX, realY)




 --子弹的飞行的时间
  local length =  math.sqrt(realX*realX+realY*realY);
  local realTime = length/400;
--动作
 local moveto =  cc.MoveTo:create(0.5,realPos);


 bullet:runAction(moveto);


print("Ended")


end




local   dispatcher = cc.Director:getInstance():getEventDispatcher();
local   listener   =  cc.EventListenerTouchOneByOne:create();
--listener:setSwallowTouches(true)
listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN);
listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED);
listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED);


dispatcher:addEventListenerWithSceneGraphPriority(listener, self);
 
end




return GameScene;









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值