Quick-cocos2d-x3.2 game Lua quick simple
Classic entry games, here with a quick-cocos2d-x3.2 re-write it, so that you are familiar with the next quick
First, create a project, if not Baidu.
1. The compilation result is as follows:
2. Set the game background to white. Let's take a look at the mainscene. Lua file.
Local mainscene = Class ("mainscene", function () return display. newscene ("mainscene") End) -- equivalent to the constructor function mainscene: ctor () in C ++ -- sets the background to white and serves as the layer of the current scenario, in order to implement the touch event self in the future. touchlayer _ = display. newcolorlayer (CC. c4B (255,255,255,255): addto (Self) endfunction mainscene: onenter () endfunction mainscene: onexit () endreturn mainscene
Effect:
Add a ninja to place it in the middle of the left of the screen, and add a method to refresh each frame. Add the following code after the ctor method.
-- Add the ninja genie self. player _ = display. newsprite ("player.png"): addto (self. touchlayer _) self. player _: pos (self. player _: getcontentsize (). width/2, display. height/2) -- initializes the array self that stores darts and enemies (monsters. projectiles _ = {} self. monsters _ ={} -- add the method self: addnodeeventlistener (CC. node_enter_frame_event, handler (self, self. update) Self: scheduleupdate ()
And then add it to mainscene. Lua.
-- Add the enemy function mainscene: addmonster () endfunction mainscene: ontouchended (x, y) end -- refresh function mainscene: Update (DT) print ("Update") End
Effect
Add a method called per second. Add the following code at the top of mainscene. Lua:
local scheduler = require(cc.PACKAGE_NAME .. ".scheduler")
Then add the following code at the bottom of the ctor method:
-- Add a method schedster. scheduleglobal (function () Self: addmonster () -- method called per second end, 1) -- 1 is the time
Add monsters as follows
-- Add the enemy function mainscene: addmonster () local monster = display. newsprite ("monster.png"): addto (self. touchlayer _) Local miny = MONSTER: getcontentsize (). height/2 Local Maxy = display. height-monster: getcontentsize (). height/2 Local rangey = Maxy-miny local actualy = math. random (rangey) MONSTER: pos (display. width + MONSTER: getcontentsize (). width/2, actualy) Local minduration = 2.0 local maxduration = 4.0 local rangeduration = maxduration-minduration local actualduration = (math. random (rangeduration) transition. moveTo (monster, {x =-monster: getcontentsize (). width/2, y = actualy, time = actualduration, oncomplete = function (event) -- delete self after execution. monsters _ [event] = nil event: removeself () end}) -- put the data that stores monsters to delete self. monsters _ [Monster] = monsterend
Add touch events at the bottom of the ctor Method
-- Add the touch event self. touchlayer _: settouchenabled (true) self. touchlayer _: addnodeeventlistener (CC. node_touch_event, function (event) If event. name = "ended" then self: ontouchended (event. x, event. y) end return true end)
Enables the motor screen to launch darts
function MainScene:onTouchEnded(x , y) print("onTouchEnded") local location = cc.p(x,y) local projectile = display.newSprite("Projectile.png"):addTo(self) projectile:pos(20 , display.height / 2) local px , py = projectile:getPosition() local offset = cc.pSub(location,cc.p(px,py)) if (offset.x <= 0) then return end local realX = display.width + projectile:getContentSize().width / 2 local ratio = offset.y / offset.x local realY = realX * ratio +py local realDest = cc.p(realX,realY) local offRealX = realX - px local offRealY = realY - py local length = math.sqrt(offRealX * offRealX + offRealY * offRealY) local velocity = 480 / 1 local realMoveDuration = length / velocity transition.moveTo(projectile,{ x = realDest.x , y = realDest.y , time = realMoveDuration , onComplete = function (event) self.projectiles_[event] = nil event:removeSelf() end }) self.projectiles_[projectile] = projectileend
In the end, the attack on the enemy's bullets and enemies disappear simultaneously.
-- Calculate the distance between two points: Local function dist (ax, ay, BX, by) Local dx, dy = Bx-ax, by-ay return math. SQRT (dx * dx + dy * Dy) end -- function mainscene: Update (DT) -- print ("Update") Local projectilestodelete ={} for k_pro, v_pro in pairs (self. projectiles _) Do Local monsterstodelete ={} for k_mon, v_mon in pairs (self. monsters _) Do Local P_x, p_y = v_pro: getposition () Local m_x, m_y = v_mon: getposition () If dist (P_x, p_y, m_x, m_y) <= 30 then monsterstodelete [v_mon] = v_mon projectilestodelete [v_pro] = v_pro end for k_m_d, v_m_d in pairs (monsterstodelete) do -- local X, Y = v_m_d: getposition () self. monsters _ [v_m_d] = nil v_m_d: removeself () end monsterstodelete = nil end for k_p_d, v_p_d in pairs (projectilestodelete) do self. projectiles _ [v_p_d] = nil v_p_d: removeself () end projectilestodelete = nilend
In this way, we have implemented this simple little game, and it is much easier to implement quick than before.
Code download: http://download.csdn.net/detail/qqmcy/8087361
Address: http://blog.csdn.net/qqmcy/article/details/40508873
Dart ninja Co., quick-cocos2d-x3.2.