参考地址:http://cn.cocos2d-x.org/tutorial/show?id=1260
local Player = class(“Player”, function()
local sprite = display.newSprite("#player1-1-1.png")
return sprite
end)
function Player:ctor()
self:addAnimation()
self.fsm_ = {}
cc.GameObject.extend(self.fsm_)
:addComponent("components.behavior.StateMachine")
:exportMethods()
self.fsm_:setupState({
-- 初始状态
initial = "idle",
-- 事件和状态转换
events = {
-- t1:clickScreen; t2:clickEnemy; t3:beKilled; t4:stop
{name = "clickScreen", from = {"idle", "attack"}, to = "walk" },
{name = "clickEnemy", from = {"idle", "walk"}, to = "attack"},
{name = "beKilled", from = {"idle", "walk", "attack"}, to = "dead"},
{name = "stop", from = {"walk", "attack"}, to = "idle"},
},
-- 状态转变后的回调
callbacks = {
onidle = function () print("msg_lyywhg: ~" .. "idle") end,
onwalk = function () print("msg_lyywhg: ~" .. "move") end,
onattack = function () print("msg_lyywhg: ~" .. "attack") end,
ondead = function () print("msg_lyywhg: ~" .. "dead") end
},
})
end
function Player:addAnimation()
local animationNames = {“walk”, “attack”, “dead”}
local animationFrameNum = {4, 4, 4}
for i = 1, #animationNames do
local frames = display.newFrames("player1-" .. i .. "-%d.png", 1, animationFrameNum*)
local animation = display.newAnimation(frames, 0.2)
display.setAnimationCache("player1-" .. animationNames*, animation)
end
end
function Player:doEvent(event)
self.fsm_:doEvent(event)
end
function Player:walkTo(pos, callback)
local function moveStop()
transition.stopTarget(self)
if callback then
callback()
end
end
local currentPos = cc.p(cc.Node.getPositionX(self), cc.Node.getPositionY(self))
local destPos = cc.p(pos.x, pos.y)
local posDiff = cc.pDistanceSQ(currentPos, destPos)
local seq = transition.sequence({cc.MoveTo:create(5 * posDiff / display.width, cc.p(pos.x,pos.y)), cc.CallFunc:create(moveStop)})
transition.playAnimationForever(self, display.getAnimationCache("player1-walk"))
self:runAction(seq)
return true
end
function Player:attack()
transition.playAnimationOnce(self, display.getAnimationCache(“player1-attack”))
end
function Player:dead()
transition.playAnimationOnce(self, display.getAnimationCache(“player1-dead”))
end
return Player
报错:
2015-03-05 10:44:59.730 player3 NSConcreteAttributedString initWithString:: nil value**
