update lua-default-template
After Width: | Height: | Size: 92 KiB |
After Width: | Height: | Size: 59 KiB |
After Width: | Height: | Size: 3.5 KiB |
After Width: | Height: | Size: 156 KiB |
After Width: | Height: | Size: 4.0 KiB |
After Width: | Height: | Size: 498 KiB |
After Width: | Height: | Size: 4.2 KiB |
Before Width: | Height: | Size: 53 KiB |
Before Width: | Height: | Size: 26 KiB |
Before Width: | Height: | Size: 113 KiB |
Before Width: | Height: | Size: 24 KiB |
Before Width: | Height: | Size: 5.8 KiB |
Before Width: | Height: | Size: 69 KiB |
|
@ -0,0 +1,8 @@
|
|||
|
||||
local MyApp = class("MyApp", cc.load("mvc").AppBase)
|
||||
|
||||
function MyApp:onCreate()
|
||||
math.randomseed(os.time())
|
||||
end
|
||||
|
||||
return MyApp
|
|
@ -0,0 +1,13 @@
|
|||
|
||||
local BugBase = import(".BugBase")
|
||||
|
||||
local BugAnt = class("BugAnt", BugBase)
|
||||
|
||||
function BugAnt:ctor()
|
||||
BugAnt.super.ctor(self)
|
||||
self.type_ = BugBase.BUG_TYPE_ANT
|
||||
self.speed_ = 1.0
|
||||
self.touchRange_ = 70
|
||||
end
|
||||
|
||||
return BugAnt
|
|
@ -0,0 +1,62 @@
|
|||
|
||||
local BugBase = class("BugBase")
|
||||
|
||||
BugBase.BUG_TYPE_ANT = "ANT"
|
||||
BugBase.BUG_TYPE_SPIDER = "SPIDER"
|
||||
|
||||
function BugBase:ctor()
|
||||
self.position_ = cc.p(0, 0)
|
||||
self.rotation_ = 0
|
||||
self.type_ = nil
|
||||
self.dist_ = 0
|
||||
self.destination_ = cc.p(0, 0)
|
||||
self.speed_ = 1
|
||||
self.touchRange_ = 0
|
||||
end
|
||||
|
||||
function BugBase:getType()
|
||||
return self.type_
|
||||
end
|
||||
|
||||
function BugBase:getPosition()
|
||||
return self.position_
|
||||
end
|
||||
|
||||
function BugBase:getRotation()
|
||||
return self.rotation_
|
||||
end
|
||||
|
||||
function BugBase:getDist()
|
||||
return self.dist_
|
||||
end
|
||||
|
||||
function BugBase:setDestination(destination)
|
||||
self.destination_ = clone(destination)
|
||||
self.dist_ = math.random(display.width / 2 + 100, display.width / 2 + 200)
|
||||
|
||||
local rotation = math.random(0, 360)
|
||||
self.position_ = self:calcPosition(rotation, self.dist_, destination)
|
||||
self.rotation_ = rotation - 180
|
||||
return self
|
||||
end
|
||||
|
||||
local fixedDeltaTime = 1.0 / 60.0
|
||||
function BugBase:step(dt)
|
||||
self.dist_ = self.dist_ - self.speed_ * (dt / fixedDeltaTime)
|
||||
self.position_ = self:calcPosition(self.rotation_ + 180, self.dist_, self.destination_)
|
||||
return self
|
||||
end
|
||||
|
||||
function BugBase:calcPosition(rotation, dist, destination)
|
||||
local radians = rotation * math.pi / 180
|
||||
return cc.p(destination.x + math.cos(radians) * dist,
|
||||
destination.y - math.sin(radians) * dist)
|
||||
end
|
||||
|
||||
function BugBase:checkTouch(x, y)
|
||||
local dx, dy = x - self.position_.x, y - self.position_.y
|
||||
local offset = math.sqrt(dx * dx + dy * dy)
|
||||
return offset <= self.touchRange_
|
||||
end
|
||||
|
||||
return BugBase
|
|
@ -0,0 +1,13 @@
|
|||
|
||||
local BugBase = import(".BugBase")
|
||||
|
||||
local BugSpider = class("BugSpider", BugBase)
|
||||
|
||||
function BugSpider:ctor()
|
||||
BugSpider.super.ctor(self)
|
||||
self.type_ = BugBase.BUG_TYPE_SPIDER
|
||||
self.speed_ = 1.5
|
||||
self.touchRange_ = 50
|
||||
end
|
||||
|
||||
return BugSpider
|
|
@ -0,0 +1,41 @@
|
|||
|
||||
local BugSprite = class("BugSprite", function(imageFilename)
|
||||
local texture = display.getImage(imageFilename)
|
||||
local frameWidth = texture:getPixelsWide() / 3
|
||||
local frameHeight = texture:getPixelsHigh()
|
||||
|
||||
local spriteFrame = display.newSpriteFrame(texture, cc.rect(0, 0, frameWidth, frameHeight))
|
||||
local sprite = display.newSprite(spriteFrame)
|
||||
sprite.animationName_ = imageFilename
|
||||
sprite.frameWidth_ = frameWidth
|
||||
sprite.frameHeight_ = frameHeight
|
||||
return sprite
|
||||
end)
|
||||
|
||||
function BugSprite:ctor(imageFilename, bugModel)
|
||||
self.model_ = bugModel
|
||||
end
|
||||
|
||||
function BugSprite:getModel()
|
||||
return self.model_
|
||||
end
|
||||
|
||||
function BugSprite:start(destination)
|
||||
self.model_:setDestination(destination)
|
||||
self:updatePosition()
|
||||
self:playAnimationForever(display.getAnimationCache(self.animationName_))
|
||||
return self
|
||||
end
|
||||
|
||||
function BugSprite:step(dt)
|
||||
self.model_:step(dt)
|
||||
self:updatePosition()
|
||||
return self
|
||||
end
|
||||
|
||||
function BugSprite:updatePosition()
|
||||
self:move(self.model_:getPosition())
|
||||
:rotate(self.model_:getRotation())
|
||||
end
|
||||
|
||||
return BugSprite
|
|
@ -0,0 +1,10 @@
|
|||
|
||||
local DeadBugSprite = class("DeadBugSprite", function(imageFilename)
|
||||
local texture = display.getImage(imageFilename)
|
||||
local frameWidth = texture:getPixelsWide() / 3
|
||||
local frameHeight = texture:getPixelsHigh()
|
||||
local spriteFrame = display.newSpriteFrame(texture, cc.rect(frameWidth * 2, 0, frameWidth, frameHeight))
|
||||
return display.newSprite(spriteFrame)
|
||||
end)
|
||||
|
||||
return DeadBugSprite
|
|
@ -0,0 +1,190 @@
|
|||
|
||||
-- GameView is a combination of view and controller
|
||||
local GameView = class("GameView", cc.load("mvc").ViewBase)
|
||||
|
||||
local BugBase = import("..models.BugBase")
|
||||
local BugAnt = import("..models.BugAnt")
|
||||
local BugSpider = import("..models.BugSpider")
|
||||
|
||||
local BugSprite = import(".BugSprite")
|
||||
local DeadBugSprite = import(".DeadBugSprite")
|
||||
|
||||
GameView.HOLE_POSITION = cc.p(display.cx - 30, display.cy - 75)
|
||||
GameView.INIT_LIVES = 5
|
||||
GameView.ADD_BUG_INTERVAL_MIN = 1
|
||||
GameView.ADD_BUG_INTERVAL_MAX = 3
|
||||
|
||||
GameView.IMAGE_FILENAMES = {}
|
||||
GameView.IMAGE_FILENAMES[BugBase.BUG_TYPE_ANT] = "BugAnt.png"
|
||||
GameView.IMAGE_FILENAMES[BugBase.BUG_TYPE_SPIDER] = "BugSpider.png"
|
||||
|
||||
GameView.BUG_ANIMATION_TIMES = {}
|
||||
GameView.BUG_ANIMATION_TIMES[BugBase.BUG_TYPE_ANT] = 0.15
|
||||
GameView.BUG_ANIMATION_TIMES[BugBase.BUG_TYPE_SPIDER] = 0.1
|
||||
|
||||
GameView.ZORDER_BUG = 100
|
||||
GameView.ZORDER_DEAD_BUG = 50
|
||||
|
||||
GameView.events = {
|
||||
PLAYER_DEAD_EVENT = "PLAYER_DEAD_EVENT",
|
||||
}
|
||||
|
||||
function GameView:start()
|
||||
self:scheduleUpdate(handler(self, self.step))
|
||||
return self
|
||||
end
|
||||
|
||||
function GameView:stop()
|
||||
self:unscheduleUpdate()
|
||||
return self
|
||||
end
|
||||
|
||||
function GameView:step(dt)
|
||||
if self.lives_ <= 0 then return end
|
||||
|
||||
self.addBugInterval_ = self.addBugInterval_ - dt
|
||||
if self.addBugInterval_ <= 0 then
|
||||
self.addBugInterval_ = math.random(GameView.ADD_BUG_INTERVAL_MIN, GameView.ADD_BUG_INTERVAL_MAX)
|
||||
self:addBug()
|
||||
end
|
||||
|
||||
for _, bug in pairs(self.bugs_) do
|
||||
bug:step(dt)
|
||||
if bug:getModel():getDist() <= 0 then
|
||||
self:bugEnterHole(bug)
|
||||
end
|
||||
end
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
function GameView:getLives()
|
||||
return self.lives_
|
||||
end
|
||||
|
||||
function GameView:getKills()
|
||||
return self.kills_
|
||||
end
|
||||
|
||||
function GameView:addBug()
|
||||
local bugType = BugBase.BUG_TYPE_ANT
|
||||
if math.random(1, 2) % 2 == 0 then
|
||||
bugType = BugBase.BUG_TYPE_SPIDER
|
||||
end
|
||||
|
||||
local bugModel
|
||||
if bugType == BugBase.BUG_TYPE_ANT then
|
||||
bugModel = BugAnt:create()
|
||||
else
|
||||
bugModel = BugSpider:create()
|
||||
end
|
||||
|
||||
local bug = BugSprite:create(GameView.IMAGE_FILENAMES[bugType], bugModel)
|
||||
:start(GameView.HOLE_POSITION)
|
||||
:addTo(self.bugsNode_, GameView.ZORDER_BUG)
|
||||
|
||||
self.bugs_[bug] = bug
|
||||
return self
|
||||
end
|
||||
|
||||
function GameView:bugEnterHole(bug)
|
||||
self.bugs_[bug] = nil
|
||||
|
||||
bug:fadeOut({time = 0.5, removeSelf = true})
|
||||
:scaleTo({time = 0.5, scale = 0.3})
|
||||
:rotateTo({time = 0.5, rotation = math.random(360, 720)})
|
||||
|
||||
self.lives_ = self.lives_ - 1
|
||||
self.livesLabel_:setString(self.lives_)
|
||||
audio.playSound("BugEnterHole.wav")
|
||||
|
||||
if self.lives_ <= 0 then
|
||||
self:dispatchEvent({name = GameView.events.PLAYER_DEAD_EVENT})
|
||||
end
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
function GameView:bugDead(bug)
|
||||
local imageFilename = GameView.IMAGE_FILENAMES[bug:getModel():getType()]
|
||||
DeadBugSprite:create(imageFilename)
|
||||
:fadeOut({time = 2.0, delay = 0.5, removeSelf = true})
|
||||
:move(bug:getPosition())
|
||||
:rotate(bug:getRotation() + 120)
|
||||
:addTo(self.bugsNode_, GameView.ZORDER_DEAD_BUG)
|
||||
|
||||
self.bugs_[bug] = nil
|
||||
bug:removeSelf()
|
||||
|
||||
self.kills_ = self.kills_ + 1
|
||||
audio.playSound("BugDead.wav")
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
function GameView:onCreate()
|
||||
self.lives_ = GameView.INIT_LIVES
|
||||
self.kills_ = 0
|
||||
self.bugs_ = {}
|
||||
self.addBugInterval_ = 0
|
||||
|
||||
-- add touch layer
|
||||
display.newLayer()
|
||||
:enableTouch(handler(self, self.onTouch))
|
||||
:addTo(self)
|
||||
|
||||
-- add background image
|
||||
display.newSprite("PlaySceneBg.jpg")
|
||||
:move(display.center)
|
||||
:addTo(self)
|
||||
|
||||
-- add bugs node
|
||||
self.bugsNode_ = display.newNode():addTo(self)
|
||||
|
||||
-- add lives icon and label
|
||||
display.newSprite("Star.png")
|
||||
:move(display.left + 50, display.top - 50)
|
||||
:addTo(self)
|
||||
self.livesLabel_ = cc.Label:createWithSystemFont(self.lives_, "Arial", 32)
|
||||
:move(display.left + 90, display.top - 50)
|
||||
:addTo(self)
|
||||
|
||||
-- create animation for bugs
|
||||
for bugType, filename in pairs(GameView.IMAGE_FILENAMES) do
|
||||
-- load image
|
||||
local texture = display.loadImage(filename)
|
||||
local frameWidth = texture:getPixelsWide() / 3
|
||||
local frameHeight = texture:getPixelsHigh()
|
||||
|
||||
-- create sprite frame based on image
|
||||
local frames = {}
|
||||
for i = 0, 1 do
|
||||
local frame = display.newSpriteFrame(texture, cc.rect(frameWidth * i, 0, frameWidth, frameHeight))
|
||||
frames[#frames + 1] = frame
|
||||
end
|
||||
|
||||
-- create animation
|
||||
local animation = display.newAnimation(frames, GameView.BUG_ANIMATION_TIMES[bugType])
|
||||
-- caching animation
|
||||
display.setAnimationCache(filename, animation)
|
||||
end
|
||||
|
||||
-- bind the "event" component
|
||||
cc.bind(self, "event")
|
||||
end
|
||||
|
||||
function GameView:onTouch(event)
|
||||
if event.name ~= "began" then return end
|
||||
local x, y = event.x, event.y
|
||||
for _, bug in pairs(self.bugs_) do
|
||||
if bug:getModel():checkTouch(x, y) then
|
||||
self:bugDead(bug)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function GameView:onCleanup()
|
||||
self:removeAllEventListeners()
|
||||
end
|
||||
|
||||
return GameView
|
|
@ -0,0 +1,20 @@
|
|||
|
||||
local MainScene = class("MainScene", cc.load("mvc").ViewBase)
|
||||
|
||||
function MainScene:onCreate()
|
||||
-- add background image
|
||||
display.newSprite("MainSceneBg.jpg")
|
||||
:move(display.center)
|
||||
:addTo(self)
|
||||
|
||||
-- add play button
|
||||
local playButton = cc.MenuItemImage:create("PlayButton.png", "PlayButton.png")
|
||||
:onClicked(function()
|
||||
self:getApp():enterScene("PlayScene")
|
||||
end)
|
||||
cc.Menu:create(playButton)
|
||||
:move(display.cx, display.cy - 200)
|
||||
:addTo(self)
|
||||
end
|
||||
|
||||
return MainScene
|
|
@ -0,0 +1,31 @@
|
|||
|
||||
local PlayScene = class("PlayScene", cc.load("mvc").ViewBase)
|
||||
|
||||
local GameView = import(".GameView")
|
||||
|
||||
function PlayScene:onCreate()
|
||||
-- create game view and add it to stage
|
||||
self.gameView_ = GameView:create()
|
||||
:addEventListener(GameView.events.PLAYER_DEAD_EVENT, handler(self, self.onPlayerDead))
|
||||
:start()
|
||||
:addTo(self)
|
||||
end
|
||||
|
||||
function PlayScene:onPlayerDead(event)
|
||||
-- add game over text
|
||||
local text = string.format("You killed %d bugs", self.gameView_:getKills())
|
||||
cc.Label:createWithSystemFont(text, "Arial", 96)
|
||||
:align(display.CENTER, display.center)
|
||||
:addTo(self)
|
||||
|
||||
-- add exit button
|
||||
local exitButton = cc.MenuItemImage:create("ExitButton.png", "ExitButton.png")
|
||||
:onClicked(function()
|
||||
self:getApp():enterScene("MainScene")
|
||||
end)
|
||||
cc.Menu:create(exitButton)
|
||||
:move(display.cx, display.cy - 200)
|
||||
:addTo(self)
|
||||
end
|
||||
|
||||
return PlayScene
|
|
@ -0,0 +1,26 @@
|
|||
|
||||
-- 0 - disable debug info, 1 - less debug info, 2 - verbose debug info
|
||||
DEBUG = 2
|
||||
|
||||
-- use framework, will disable all deprecated API, false - use legacy API
|
||||
CC_USE_FRAMEWORK = true
|
||||
|
||||
-- show FPS on screen
|
||||
CC_SHOW_FPS = true
|
||||
|
||||
-- disable create unexpected global variable
|
||||
CC_DISABLE_GLOBAL = true
|
||||
|
||||
-- for module display
|
||||
CC_DESIGN_RESOLUTION = {
|
||||
width = 960,
|
||||
height = 640,
|
||||
autoscale = "FIXED_HEIGHT",
|
||||
callback = function(framesize)
|
||||
local ratio = framesize.width / framesize.height
|
||||
if ratio <= 1.34 then
|
||||
-- iPad 768*1024(1536*2048) is 4:3 screen
|
||||
return {autoscale = "FIXED_WIDTH"}
|
||||
end
|
||||
end
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
function myadd(x, y)
|
||||
return x + y
|
||||
end
|
|
@ -1,249 +1,14 @@
|
|||
|
||||
cc.FileUtils:getInstance():addSearchPath("src")
|
||||
cc.FileUtils:getInstance():addSearchPath("res")
|
||||
cc.FileUtils:getInstance():setPopupNotify(false)
|
||||
|
||||
-- CC_USE_DEPRECATED_API = true
|
||||
require "config"
|
||||
require "cocos.init"
|
||||
|
||||
-- cclog
|
||||
cclog = function(...)
|
||||
print(string.format(...))
|
||||
end
|
||||
|
||||
-- for CCLuaEngine traceback
|
||||
function __G__TRACKBACK__(msg)
|
||||
cclog("----------------------------------------")
|
||||
cclog("LUA ERROR: " .. tostring(msg) .. "\n")
|
||||
cclog(debug.traceback())
|
||||
cclog("----------------------------------------")
|
||||
return msg
|
||||
end
|
||||
|
||||
local function main()
|
||||
collectgarbage("collect")
|
||||
-- avoid memory leak
|
||||
collectgarbage("setpause", 100)
|
||||
collectgarbage("setstepmul", 5000)
|
||||
|
||||
-- initialize director
|
||||
local director = cc.Director:getInstance()
|
||||
local glview = director:getOpenGLView()
|
||||
if nil == glview then
|
||||
glview = cc.GLViewImpl:createWithRect("HelloLua", cc.rect(0,0,900,640))
|
||||
director:setOpenGLView(glview)
|
||||
end
|
||||
|
||||
glview:setDesignResolutionSize(480, 320, cc.ResolutionPolicy.NO_BORDER)
|
||||
|
||||
--turn on display FPS
|
||||
director:setDisplayStats(true)
|
||||
|
||||
--set FPS. the default value is 1.0/60 if you don't call this
|
||||
director:setAnimationInterval(1.0 / 60)
|
||||
|
||||
local schedulerID = 0
|
||||
--support debug
|
||||
local targetPlatform = cc.Application:getInstance():getTargetPlatform()
|
||||
if (cc.PLATFORM_OS_IPHONE == targetPlatform) or (cc.PLATFORM_OS_IPAD == targetPlatform) or
|
||||
(cc.PLATFORM_OS_ANDROID == targetPlatform) or (cc.PLATFORM_OS_WINDOWS == targetPlatform) or
|
||||
(cc.PLATFORM_OS_MAC == targetPlatform) then
|
||||
cclog("result is ")
|
||||
--require('debugger')()
|
||||
|
||||
end
|
||||
require "hello2"
|
||||
cclog("result is " .. myadd(1, 1))
|
||||
|
||||
---------------
|
||||
|
||||
local visibleSize = cc.Director:getInstance():getVisibleSize()
|
||||
local origin = cc.Director:getInstance():getVisibleOrigin()
|
||||
|
||||
-- add the moving dog
|
||||
local function createDog()
|
||||
local frameWidth = 105
|
||||
local frameHeight = 95
|
||||
|
||||
-- create dog animate
|
||||
local textureDog = cc.Director:getInstance():getTextureCache():addImage("dog.png")
|
||||
local rect = cc.rect(0, 0, frameWidth, frameHeight)
|
||||
local frame0 = cc.SpriteFrame:createWithTexture(textureDog, rect)
|
||||
rect = cc.rect(frameWidth, 0, frameWidth, frameHeight)
|
||||
local frame1 = cc.SpriteFrame:createWithTexture(textureDog, rect)
|
||||
|
||||
local spriteDog = cc.Sprite:createWithSpriteFrame(frame0)
|
||||
spriteDog.isPaused = false
|
||||
spriteDog:setPosition(origin.x, origin.y + visibleSize.height / 4 * 3)
|
||||
--[[
|
||||
local animFrames = CCArray:create()
|
||||
|
||||
animFrames:addObject(frame0)
|
||||
animFrames:addObject(frame1)
|
||||
]]--
|
||||
|
||||
local animation = cc.Animation:createWithSpriteFrames({frame0,frame1}, 0.5)
|
||||
local animate = cc.Animate:create(animation);
|
||||
spriteDog:runAction(cc.RepeatForever:create(animate))
|
||||
|
||||
-- moving dog at every frame
|
||||
local function tick()
|
||||
if spriteDog.isPaused then return end
|
||||
local x, y = spriteDog:getPosition()
|
||||
if x > origin.x + visibleSize.width then
|
||||
x = origin.x
|
||||
else
|
||||
x = x + 1
|
||||
end
|
||||
|
||||
spriteDog:setPositionX(x)
|
||||
end
|
||||
|
||||
schedulerID = cc.Director:getInstance():getScheduler():scheduleScriptFunc(tick, 0, false)
|
||||
|
||||
return spriteDog
|
||||
end
|
||||
|
||||
-- create farm
|
||||
local function createLayerFarm()
|
||||
local layerFarm = cc.Layer:create()
|
||||
|
||||
-- add in farm background
|
||||
local bg = cc.Sprite:create("farm.jpg")
|
||||
bg:setPosition(origin.x + visibleSize.width / 2 + 80, origin.y + visibleSize.height / 2)
|
||||
layerFarm:addChild(bg)
|
||||
|
||||
-- add land sprite
|
||||
for i = 0, 3 do
|
||||
for j = 0, 1 do
|
||||
local spriteLand = cc.Sprite:create("land.png")
|
||||
spriteLand:setPosition(200 + j * 180 - i % 2 * 90, 10 + i * 95 / 2)
|
||||
layerFarm:addChild(spriteLand)
|
||||
end
|
||||
end
|
||||
|
||||
-- add crop
|
||||
local frameCrop = cc.SpriteFrame:create("crop.png", cc.rect(0, 0, 105, 95))
|
||||
for i = 0, 3 do
|
||||
for j = 0, 1 do
|
||||
local spriteCrop = cc.Sprite:createWithSpriteFrame(frameCrop);
|
||||
spriteCrop:setPosition(10 + 200 + j * 180 - i % 2 * 90, 30 + 10 + i * 95 / 2)
|
||||
layerFarm:addChild(spriteCrop)
|
||||
end
|
||||
end
|
||||
|
||||
-- add moving dog
|
||||
local spriteDog = createDog()
|
||||
layerFarm:addChild(spriteDog)
|
||||
|
||||
-- handing touch events
|
||||
local touchBeginPoint = nil
|
||||
local function onTouchBegan(touch, event)
|
||||
local location = touch:getLocation()
|
||||
cclog("onTouchBegan: %0.2f, %0.2f", location.x, location.y)
|
||||
touchBeginPoint = {x = location.x, y = location.y}
|
||||
spriteDog.isPaused = true
|
||||
-- CCTOUCHBEGAN event must return true
|
||||
return true
|
||||
end
|
||||
|
||||
local function onTouchMoved(touch, event)
|
||||
local location = touch:getLocation()
|
||||
cclog("onTouchMoved: %0.2f, %0.2f", location.x, location.y)
|
||||
if touchBeginPoint then
|
||||
local cx, cy = layerFarm:getPosition()
|
||||
layerFarm:setPosition(cx + location.x - touchBeginPoint.x,
|
||||
cy + location.y - touchBeginPoint.y)
|
||||
touchBeginPoint = {x = location.x, y = location.y}
|
||||
end
|
||||
end
|
||||
|
||||
local function onTouchEnded(touch, event)
|
||||
local location = touch:getLocation()
|
||||
cclog("onTouchEnded: %0.2f, %0.2f", location.x, location.y)
|
||||
touchBeginPoint = nil
|
||||
spriteDog.isPaused = false
|
||||
end
|
||||
|
||||
local listener = cc.EventListenerTouchOneByOne:create()
|
||||
listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )
|
||||
listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED )
|
||||
listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED )
|
||||
local eventDispatcher = layerFarm:getEventDispatcher()
|
||||
eventDispatcher:addEventListenerWithSceneGraphPriority(listener, layerFarm)
|
||||
|
||||
local function onNodeEvent(event)
|
||||
if "exit" == event then
|
||||
cc.Director:getInstance():getScheduler():unscheduleScriptEntry(schedulerID)
|
||||
end
|
||||
end
|
||||
layerFarm:registerScriptHandler(onNodeEvent)
|
||||
|
||||
return layerFarm
|
||||
end
|
||||
|
||||
|
||||
-- create menu
|
||||
local function createLayerMenu()
|
||||
local layerMenu = cc.Layer:create()
|
||||
|
||||
local menuPopup, menuTools, effectID
|
||||
|
||||
local function menuCallbackClosePopup()
|
||||
-- stop test sound effect
|
||||
cc.SimpleAudioEngine:getInstance():stopEffect(effectID)
|
||||
menuPopup:setVisible(false)
|
||||
end
|
||||
|
||||
local function menuCallbackOpenPopup()
|
||||
-- loop test sound effect
|
||||
local effectPath = cc.FileUtils:getInstance():fullPathForFilename("effect1.wav")
|
||||
effectID = cc.SimpleAudioEngine:getInstance():playEffect(effectPath)
|
||||
menuPopup:setVisible(true)
|
||||
end
|
||||
|
||||
-- add a popup menu
|
||||
local menuPopupItem = cc.MenuItemImage:create("menu2.png", "menu2.png")
|
||||
menuPopupItem:setPosition(0, 0)
|
||||
menuPopupItem:registerScriptTapHandler(menuCallbackClosePopup)
|
||||
menuPopup = cc.Menu:create(menuPopupItem)
|
||||
menuPopup:setPosition(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2)
|
||||
menuPopup:setVisible(false)
|
||||
layerMenu:addChild(menuPopup)
|
||||
|
||||
-- add the left-bottom "tools" menu to invoke menuPopup
|
||||
local menuToolsItem = cc.MenuItemImage:create("menu1.png", "menu1.png")
|
||||
menuToolsItem:setPosition(0, 0)
|
||||
menuToolsItem:registerScriptTapHandler(menuCallbackOpenPopup)
|
||||
menuTools = cc.Menu:create(menuToolsItem)
|
||||
local itemWidth = menuToolsItem:getContentSize().width
|
||||
local itemHeight = menuToolsItem:getContentSize().height
|
||||
menuTools:setPosition(origin.x + itemWidth/2, origin.y + itemHeight/2)
|
||||
layerMenu:addChild(menuTools)
|
||||
|
||||
return layerMenu
|
||||
end
|
||||
|
||||
-- play background music, preload effect
|
||||
local bgMusicPath = cc.FileUtils:getInstance():fullPathForFilename("background.mp3")
|
||||
cc.SimpleAudioEngine:getInstance():playMusic(bgMusicPath, true)
|
||||
local effectPath = cc.FileUtils:getInstance():fullPathForFilename("effect1.wav")
|
||||
cc.SimpleAudioEngine:getInstance():preloadEffect(effectPath)
|
||||
|
||||
-- run
|
||||
local sceneGame = cc.Scene:create()
|
||||
sceneGame:addChild(createLayerFarm())
|
||||
sceneGame:addChild(createLayerMenu())
|
||||
|
||||
if cc.Director:getInstance():getRunningScene() then
|
||||
cc.Director:getInstance():replaceScene(sceneGame)
|
||||
else
|
||||
cc.Director:getInstance():runWithScene(sceneGame)
|
||||
end
|
||||
|
||||
require("app.MyApp"):create():run()
|
||||
end
|
||||
|
||||
|
||||
local status, msg = xpcall(main, __G__TRACKBACK__)
|
||||
if not status then
|
||||
error(msg)
|
||||
print(msg)
|
||||
end
|
||||
|
|
|
@ -0,0 +1,67 @@
|
|||
|
||||
local AppBase = class("AppBase")
|
||||
|
||||
function AppBase:ctor(configs)
|
||||
self.configs_ = {
|
||||
viewsRoot = "app.views",
|
||||
modelsRoot = "app.models",
|
||||
defaultSceneName = "MainScene",
|
||||
}
|
||||
|
||||
for k, v in pairs(configs or {}) do
|
||||
self.configs_[k] = v
|
||||
end
|
||||
|
||||
if type(self.configs_.viewsRoot) ~= "table" then
|
||||
self.configs_.viewsRoot = {self.configs_.viewsRoot}
|
||||
end
|
||||
if type(self.configs_.modelsRoot) ~= "table" then
|
||||
self.configs_.modelsRoot = {self.configs_.modelsRoot}
|
||||
end
|
||||
|
||||
if DEBUG > 1 then
|
||||
dump(self.configs_, "AppBase configs")
|
||||
end
|
||||
|
||||
if CC_SHOW_FPS then
|
||||
cc.Director:getInstance():setDisplayStats(true)
|
||||
end
|
||||
|
||||
-- event
|
||||
self:onCreate()
|
||||
end
|
||||
|
||||
function AppBase:run(initSceneName)
|
||||
initSceneName = initSceneName or self.configs_.defaultSceneName
|
||||
self:enterScene(initSceneName)
|
||||
end
|
||||
|
||||
function AppBase:enterScene(sceneName, transition, time, more)
|
||||
local view = self:createView(sceneName)
|
||||
view:showWithScene(transition, time, more)
|
||||
return view
|
||||
end
|
||||
|
||||
function AppBase:createView(name)
|
||||
for _, root in ipairs(self.configs_.viewsRoot) do
|
||||
local packageName = string.format("%s.%s", root, name)
|
||||
local status, view = xpcall(function()
|
||||
return require(packageName)
|
||||
end, function(msg)
|
||||
if not string.find(msg, string.format("'%s' not found:", packageName)) then
|
||||
print("load view error: ", msg)
|
||||
end
|
||||
end)
|
||||
local t = type(view)
|
||||
if status and (t == "table" or t == "userdata") then
|
||||
return view:create(self, name)
|
||||
end
|
||||
end
|
||||
error(string.format("AppBase:createView() - not found view \"%s\" in search paths \"%s\"",
|
||||
name, table.concat(self.configs_.viewsRoot, ",")), 0)
|
||||
end
|
||||
|
||||
function AppBase:onCreate()
|
||||
end
|
||||
|
||||
return AppBase
|
|
@ -0,0 +1,27 @@
|
|||
|
||||
local ViewBase = class("ViewBase", cc.Node)
|
||||
|
||||
function ViewBase:ctor(app, name)
|
||||
self:enableNodeEvents()
|
||||
self.app_ = app
|
||||
self.name_ = name
|
||||
if self.onCreate then self:onCreate() end
|
||||
end
|
||||
|
||||
function ViewBase:getApp()
|
||||
return self.app_
|
||||
end
|
||||
|
||||
function ViewBase:getName()
|
||||
return self.name_
|
||||
end
|
||||
|
||||
function ViewBase:showWithScene(transition, time, more)
|
||||
self:setVisible(true)
|
||||
local scene = display.newScene(self.name_)
|
||||
scene:addChild(self)
|
||||
display.runScene(scene, transition, time, more)
|
||||
return self
|
||||
end
|
||||
|
||||
return ViewBase
|
|
@ -0,0 +1,7 @@
|
|||
|
||||
local _M = {}
|
||||
|
||||
_M.AppBase = import(".AppBase")
|
||||
_M.ViewBase = import(".ViewBase")
|
||||
|
||||
return _M
|