----------MyApp.lua
require("config")
require("cocos.init")
require("framework.init")
local MyApp = class("MyApp", cc.mvc.AppBase)
function MyApp:ctor()
MyApp.super.ctor(self)
end
function MyApp:run()
cc.FileUtils:getInstance():addSearchPath("res/")
display.addSpriteFrames("1009_role.plist","1009_role.png");
self:enterScene("MainScene")
end
return MyApp
----------MainScene.lua
require("app.scenes.Data")
require("app.scenes.yaogan")
local MainScene = class("MainScene", function()
return display.newScene("MainScene")
end)
function MainScene:ctor()
local sprite = cc.Sprite:create("back.jpg");
sprite:setPosition(480,240)
self:addChild(sprite);
local yaogan = require("app.scenes.yaogan")
local layer = yaogan.new()
self:addChild(layer)
local yxiong = require("app.scenes.Player")
self.xiong = yxiong:new() --声明为类的成员,方便在触摸结束的时候调用
self.xiong:setPosition(480,240)
self:addChild(self.xiong)
local fallow = cc.Follow:create(self.xiong,cc.rect(0,0,1820,640))
self:runAction(fallow)
cc.ui.UIPushButton.new()
:setButtonLabel(cc.ui.UILabel.new({text = "walk", size = 32, color = display.COLOR_WHITE}))
:onButtonClicked(function()
print("1");
if self.xiong:canToState("walkAction") then
self.xiong:setState("walkAction")
end
end)
:align(display.CENTER, 250, 100)
:addTo(self)
--2
cc.ui.UIPushButton.new()
:setButtonLabel(cc.ui.UILabel.new({text = "hurt", size = 32, color = display.COLOR_WHITE}))
:onButtonClicked(function()
print("2");
if self.xiong:canToState("deadAction") then
self.xiong:setState("deadAction")
end
end)
:align(display.CENTER, 350, 100)
:addTo(self)
--3
cc.ui.UIPushButton.new()
:setButtonLabel(cc.ui.UILabel.new({text = "dead", size = 32, color = display.COLOR_WHITE}))
:onButtonClicked(function()
print("3");
if self.xiong:canToState("standAction") then
self.xiong:setState("standAction")
end
end)
:align(display.CENTER, 450, 100)
:addTo(self)
--4
cc.ui.UIPushButton.new()
:setButtonLabel(cc.ui.UILabel.new({text = "attackA", size = 32, color = display.COLOR_WHITE}))
:onButtonClicked(function()
print("4");
if self.xiong:canToState("winAction") then
self.xiong:setState("winAction")
end
end)
:align(display.CENTER, 550, 100)
:addTo(self)
--5
cc.ui.UIPushButton.new()
:setButtonLabel(cc.ui.UILabel.new({text = "attackb", size = 32, color = display.COLOR_WHITE}))
:onButtonClicked(function()
print("5");
if self.xiong:canToState("attackPAction") then
self.xiong:setState("attackPAction")
end
end)
:align(display.CENTER, 650, 100)
:addTo(self)
self:scheduleDemo()
end
function MainScene:onEnter()
end
function MainScene:onExit()
end
function MainScene:scheduleDemo( )
if Data.Direction=="rightUp" then
local move = cc.MoveBy:create(0.3,cc.p(10,10))
local move2 = cc.MoveBy:create(0.3,cc.p(10,10))
local call = cc.CallFunc:create(function ( )
self.xiong:setFlippedX(true)
end)
local seq = cc.Sequence:create(move,call,move2)
self.xiong:walkAction()
self.xiong:runAction(seq)
end
end
return MainScene
---------Player.lua
local Player = class("Player",function ( )
return display.newSprite("#1009_role/1000")
end)
function Player:ctor( )
local StateMachine = require("framework.cc.components.behavior.StateMachine")
self.fsm = StateMachine.new() --创建状态机
self.state = "none"
self.fsm:setupState({
events =
{
{name="walkAction",from={"none","stand"},to="walk"},
{name="deadAction",from={"none","attackP","attackA","attackB","attackC","attackD","stand","walk"},to="dead"},
{name="standAction",from={"none","attackP","attackA","attackB","attackC","attackD","walk"},to="stand"},
{name="winAction",from={"none","attackP","attackA","attackB","attackC","attackD","stand","walk"},to="win"},
{name="attackPAction",from={"stand","walk"},to="attackP"},
{name="attackAAction",from={"stand","walk"},to="attackA"},
{name="attackBAction",from={"stand","walk"},to="attackB"},
{name="attackCAction",from={"stand","walk"},to="attackC"},
{name="attackDAction",from={"stand","walk"},to="attackD"},
},
callbacks =
{
onbeforewalkAction = function(event) self:stopAllActions() end,--跑动
onwalkAction = function(event) self:walkAction( ) end,
onleavewalkAction = function(event) end,
onbeforestandAction = function(event) self:stopAllActions() end,--站立
onstandAction = function(event)
self:standAction()
end,
onleavehurtAction = function(event) end,
onbeforedeadAction = function(event) self:stopAllActions() end,--死亡
ondeadAction = function(event)self:deadAction() end,
onleavedeadAction = function(event) end,
onbeforewinAction = function(event) self:stopAllActions() end,--获胜
onwinAction = function(event)
self:winAction() end,
onleaveattackAAction = function(event) end,
onbeforeattackPAction = function(event) self:stopAllActions(); end,--普攻
onattackPAction = function(event) self:attackPAction()
end,
onleaveattackBAction = function(event) end,
onbeforeattackAAction = function(event) self:stopAllActions() end,--大棒技能
onattackAAction = function(event)
self:attackAAction() end,
onleaveattackAAction = function(event) end,
onbeforeattackBAction = function(event) self:stopAllActions() end,--翻滚打怪
onattackBAction = function(event)
self:attackBAction() end,
onleaveattackAAction = function(event) end,
onbeforeattackCAction = function(event) self:stopAllActions() end,--分身打怪
onattackCAction = function(event)
self:attackCAction() end,
onleaveattackAAction = function(event) end,
onbeforeattackDAction = function(event) self:stopAllActions() end,--旋转打怪
onattackDAction = function(event)
self:attackDAction() end,
onleaveattackAAction = function(event) end,
},
});
end
function Player:walkAction( )
local frames = display.newFrames("1009_role/00%i", 27, 17)--跑动
local animation = display.newAnimation(frames, 0.5 / 4)
local animate = cc.Animate:create(animation)
self:runAction(cc.RepeatForever:create(animate))
-- self:playAnimationForever(animation)
end
function Player:deadAction( )
local frames = display.newFrames("1009_role/03%i", 45, 8)--死亡
local animation = display.newAnimation(frames, 0.5 / 4)
self:playAnimationForever(animation)
end
function Player:standAction( )
local frames = display.newFrames("1009_role/100%i", 0, 9)--行走
local animation = display.newAnimation(frames, 0.5 / 4)
self:playAnimationForever(animation)
end
function Player:winAction( )
local frames = display.newFrames("1009_role/0%i", 275, 63)--旋转
local animation = display.newAnimation(frames, 0.5 / 4)
self:playAnimationForever(animation)
end
function Player:attackPAction( )
local frames = display.newFrames("1009_role/10%i", 45,130)--普攻
local animation = display.newAnimation(frames, 0.5 / 4)
self:playAnimationForever(animation)
end
function Player:attackAAction( )
local frames = display.newFrames("1009_role/01%i", 81,18)--大棒技能
local animation = display.newAnimation(frames, 0.5 / 4)
self:playAnimationForever(animation)
end
function Player:attackBAction( )
local frames = display.newFrames("1009_role/020%i", 0, 45)--翻滚打怪
local animation = display.newAnimation(frames, 0.5 / 4)
self:playAnimationForever(animation)
end
function Player:attackCAction( )
local frames = display.newFrames("1009_role/02%i", 45, 26)--分身打怪
local animation = display.newAnimation(frames, 0.5 / 4)
self:playAnimationForever(animation)
end
function Player:attackDAction( )
local frames = display.newFrames("1009_role/0%i", 275, 63)--旋转打怪
local animation = display.newAnimation(frames, 0.5 / 4)
self:playAnimationForever(animation)
end
function Player:setState(s)
print(s)
print(self.state)
if s == self.state then
return
end
self.fsm:doEvent(s)
end
function Player:canToState(s)
return self.fsm:canDoEvent(s)
end
return Player
-------yaogan.lua
require("app.scenes.Data")
require("app.scenes.Player")
local yaogan = class("yaogan",function ()
return display.newLayer("yaogan")
end)
function yaogan:ctor( )
sprite = cc.Sprite:create("yaogan.png")
sprite:setPosition(100,100)
self:addChild(sprite,1)
yaodian = cc.Sprite:create("yaodian.png")
yaodian:setPosition(cc.p(100,100))
self:addChild(yaodian,2)
function onTouchBegan(touch, event)
local r1 = touch:getLocation()--触摸点
local r2 = yaodian:getBoundingBox() --得到矩形
if cc.rectContainsPoint(r2,r1) then--矩形包含
return true
end
return false
end
function onTouchMoved(touch, event)
local t1 = sprite:getPositionX() --摇杆圆心位置
local t2 = sprite:getPositionY()
local t = touch:getLocation();--触摸位置
local v = sprite:getBoundingBox();--摇杆矩形范围
local r = sprite:getContentSize().width/2-30;--半径
local distan = math.sqrt(math.pow((t.x-t1),2)+math.pow((t.y-t2),2));
local dd = r/distan;
local x = t.x-t1
local y = t.y-t2
yaodian:setPosition(t)
if distan>r then
--停留在边界
local pp = cc.p(x*dd,y*dd);
yaodian:setPosition(cc.p(pp.x+t1,pp.y+t2))
else
yaodian:setPosition(t)
end
local x = t.x-t1
local y = t.y-t2
local d = (t.x-t2)/distan;
if y<10 and y>-10 and x>0 then
--向右
Data.Direction="right"
Data.directionfile=false
print("向右")
elseif y>10 and x>10 then
--右上
Data.Direction="rightUp"
Data.directionfile=false
print("右上")
elseif x<10 and x>-10 and y>0 then
--上
Data.Direction="Up"
Data.directionfile=false
elseif x<-10 and y>10 then
--左上
Data.Direction="lefup"
Data.directionfile=true
elseif y<10 and y>-10 and x<0 then
Data.Direction="left"
Data.directionfile=true
elseif y<-10 and x<-10 then
Data.Direction="leftDown"
Data.directionfile=true
elseif x<10 and x>-10 and y<0 then
Data.Direction="Down"
Data.directionfile=false
elseif y<-10 and x>10 then
Data.Direction="rightDown"
Data.directionfile=false
else
Data.Direction=nil
end
end
function onTouchEnded(touch, event)
local t1 = sprite:getPositionX()
local t2 = sprite:getPositionY()
yaodian:setPosition(cc.p(t1,t2))
self:getParent().xiong:setState("standAction")
end
local d = cc.Director:getInstance():getEventDispatcher()
local l = cc.EventListenerTouchOneByOne:create()
l:setSwallowTouches(true) --设置吞噬
l:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN)
l:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED)
l:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED)
d:addEventListenerWithSceneGraphPriority(l, self)
end
return yaogan
----Data.lua
module("Data",package.seeall)
Direction=nil
directionfile=false