摇杆与状态机

----------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

时间: 2024-11-17 10:06:13

摇杆与状态机的相关文章

简易状态机

SimpleFSM 包含状态切换以及事件驱动传递参数 下面的代码是登录的状态码 1 using System; 2 using UnityEngine; 3 using System.Collections; 4 5 public class LoginState : SingletonPrivider<LoginState>, GameState 6 { 7 private delegate void LoginEventHandler(object sender, LoginEventAr

MediaPlayer 状态机 API 详解 示例

简介 public class android.media.MediaPlayer extends Object implements VolumeAutomation 可能需要的权限: One may need to declare a corresponding(相应) WAKE_LOCK permission <uses-permission> element. <uses-permission android:name="android.permission.WAKE_

人生就是一个状态机

从出生到死亡人生走的是一个过程. 从宏观来看,人分为幼年.青年.中年和老年:从微观来看.人每天吃饭.睡觉.学习.工作和娱乐.古语有云:良田千倾只是一日三餐.广厦万间仅仅睡卧榻三尺.不是必需我一己私利而贪得无厌.而我看来人生就如同一个大型状态机. 人生的状态机从大的方面看,它的输入为时间,输出为做出的成果.而人生的不同年龄为所处的状态.时间我们能够看做连续的,也能够看做离散的.时间在不知不觉中流逝,也在分秒钟跳变.而时间人生的输入,他使我们从孩童成长为青年,又使我们从青年变为老年,时间不会停止.输

停车场门禁控制系统的状态机

其状态机如下图: #include<iostream>using namespace std;void main(){ char enter_or_out; //1表示入闸传感器ture,0表示出闸传感器ture int up_or_downt = 0; //1表示起落杆升起,0表示起落杆落下 cout << "状态初始化..." << '\n'; cout << "当前状态为起落杆落下,红灯状态,禁止通行" <

UNITY 状态机 + SVN + 码云 下篇

上篇说到自己写的一个FSM状态机,这篇写怎么把代码和码云联系在一起! 首先,我们应该知道为什么使用码云? 码云是开源中国社区2013年推出的基于 Git 的完全免费的代码托管服务,这个服务是基于 Gitlab 开源软件所开发的,我们在 Gitlab 的基础上做了大量的改进和定制开发,目前已经成为国内最大的代码托管系统,致力于为国内开发者提供优质稳定的托管服务. 码云除了提供最基础的 Git 代码托管之外,还提供代码在线查看.历史版本查看.Fork.Pull Request.打包下载任意版本.Is

001-初识状态机

状态机 FPGA的灵魂 状态机的设计贯穿FPGA设计的始终 一.状态机的概念 1.状态机简述 状态机:描述状态变迁的状态转移图,体现系统对外界事件的反应和行为. 有限状态机(FSM):状态节点数和输入.输出范围有限的状态机. 2.状态机的组成六要素 (1)状态集合   (必备要素):包含了状态机所能达到的所有状态. (2)初态         (必备要素):是整个状态机开始工作的起点.初态是一个相对的参考态. (3)终态   () :状态机的结束状态.事实上,大部分运行在FPGA上的状态机是没有

【slighttpd】基于lighttpd架构的Server项目实战(8)—状态机机制回顾

有限状态机FSM(Finite State Machine) 关于状态机的一个极度确切的描述是它是一个有向图形,由一组节点和一组相应的转移函数组成.状态机通过响应一系列事件而"运行".每个事件都在属于"当前" 节点的转移函数的控制范围内,其中函数的范围是节点的一个子集.函数返回"下一个"(也许是同一个)节点.这些节点中至少有一个必须是终态.当到达终态, 状态机停止. 传统应用程序的控制流程基本是顺序的:遵循事先设定的逻辑,从头到尾地执行.很少有事

简单的虚拟摇杆控制移动(NGUI)

一.用NGUI创建虚拟摇杆贴图 先创建一个sprite作为背景叫做JoyStick 并添加一个BoxCollider,再创建一个sprite child作为虚拟摇杆中间的按钮,叫做button 二.通过虚拟摇杆获得x,y偏移值 1 using UnityEngine; 2 using System.Collections; 3 4 public class JoyStick : MonoBehaviour 5 { 6 7 private bool isPress = false; 8 priva

三、状态机的设计指导原则

---恢复内容开始--- 1.状态机设计关键是什么? 如何才能把一个电路系统抽象为一个或者多个相互配合嵌套的状态机和组合系统模块?是关键. ---恢复内容结束--- 1.状态机设计关键是什么? 如何才能把一个电路系统抽象为一个或者多个相互配合嵌套的状态机和组合系统模块?是关键.