RPG Game state Machine Implementation Example

Source: Internet
Author: User
Tags addchild fsm
Local Hero = Class ("Hero", function () return Display.newnode () end) function Hero:ctor () Self:inithero () Self:add StateMachine ()--Add a state machine function End Function Hero:inithero () Self.hero = sp.
	Skeletonanimation:create ("Spine/sp-zuozhu/skeleton.json", "Spine/sp-zuozhu/skeleton.atlas", 1)
	Self.hero.debugBones = true; Self.jineng = sp. Skeletonanimation:create ("Spine/sp-zuozhu/skeleton_effect.json", "Spine/sp-zuozhu/skeleton_effect.atlas", 1) Self : AddChild (Self.hero) self:addchild (Self.jineng)--Create Spine skeletal animation wizard self.sk = sp. Skeletonanimation:create ("Spine/effects/huangseguanghuan/huangseguanghuan.json", "spine/effects/ Huangseguanghuan/huangseguanghuan.atlas ", 1) self.sk:setAnimation (0," animation ", True) Self:addchild (self.sk)-- Add animation self.hero:setAnimation (0, "Run", true) end--Increase the state machine function hero:addstatemachine ()--Table SELF.FSM = {} c C.gameobject.extend (SELF.FSM): AddComponent ("Components.behavior.StateMachine"): Exportmethods ()--Creates a state machine object self. Fsm:setupstate ({--The initialization state is idle initial = "idle", events = {--the corresponding event when the state transitions {name = "normal", from={"Walk", "Run", "Atk1", "Atk2", "Atk3", "Skill1", "Skill2", "Skill3", "Floorhurt", "Win"}, to = "Idle"}, {name = "move1", from={"Idle", "Run"}, T o = "Walk"}, {name = "Move2", from={"idle", "Walk"}, to = "Run"}, {name = "Attack1", from={"idle", "Walk", "Run"}, to = "Atk1"}, {name = ' Attack2 ', from={"idle", "Walk", "Run", "Atk1"}, to = "Atk2"}, {name = "Attac K3 ", from={" idle "," Walk "," Run "," Atk2 "}, to=" Atk3 "}, {name =" SKILLS1 ", from={" idle "," Walk "," Run "}, to =" skill1  "}, {name =" Skills2 ", from={" idle "," Walk "," Run "}, to =" Skill2 "}, {name =" SKILLS3 ", from={" idle "," Walk ", "Run"}, to = "Skill3"}, {name = "injured", from={"idle", "Walk", "Run"}, to= "Floorhurt"}, {name = "death", F rom={"Idle", "Walk", "Run", "Atk1", "Atk2", "Atk3", "Skill1", "Skill2", "Skill3"}, to = "Die"}, {name = "victory", from={"IDL E "," Walk "," Run "," Atk1"," Atk2 "}, to =" Win "},--from and to are followed by state, to the post-jump state callbacks={-The callback function when the transition occurs ONENTERIDLE=FUNCT Ion () self.hero:setAnimation (0, "idle", true) end, Onenterwalk = function () self.hero:setAnimatio
	        N (0, "Walk", true) end, Onenterrun = function () self.hero:setAnimation (0, "Run", true) End, Onenteratk1 = function () self.hero:setAnimation (0, "Atk1", false) Self.jineng:setAni Mation (0, "Atk1", false) Self.isputskills = true--Self.scheduler = Scheduler.scheduleglobal (Handler (SE LF, self.
	        	pengzhuangjiance), 0.1) end, onenteratk2 = function () self.hero:setAnimation (0, "atk2", false) Self.jineng:setAnimation (0, "atk2", false) Self.isputskills = true--Self.scheduler = Schedule R.scheduleglobal (Handler (self, self. pengzhuangjiance), 0.1) end, ONENTERATK3 = function () Self.hero:setAnimatIon (0, "Atk3", False) self.jineng:setAnimation (0, "Atk3", false) Self.isputskills = true--Self . Scheduler = Scheduler.scheduleglobal (Handler (self, self. pengzhuangjiance), 0.1) end, onenterskill1 = function ()--wait until local a1 = cc. Delaytime:create (0.2) Local a2 = cc. Moveby:create (0.5,CC.P (480* (-self.hero:getscalex ()), 0))--Local S = transition.sequence (A1,A2) Local s = cc. Sequence:create (A1,a2,nil) self:runaction (s) self.hero:setAnimation (0, "Skill1", false) SELF.J
	        	Ineng:setanimation (0, "Skill1", false) Self.isputskills = True end, Onenterskill2 = function () Self.hero:setAnimation (0, "Skill2", False) self.jineng:setAnimation (0, "Skill2", false) Self.isp
	        	Utskills = True end, Onenterskill3 = function () self.hero:setAnimation (0, "Skill3", false) Self.jineng:setAnimation (0, "Skill3",False) Self.isputskills = True end, Onenterfloorhurt = function () Self.hero:setAnim ation (0, "Floorhurt", false) end, Onenterdie = function () self.hero:clearTracks () se
	        Lf.jineng:clearTracks () self.hero:setAnimation (0, "die", False) self.jineng:setAnimation (0, "die", false) End, Onenterwin = function () self.hero:setAnimation (0, "win", false) Self.jineng:setA Nimation (0, "win", false) End,},}) End Function Hero:doevent (event)--Spine wizard object can be called Doevent ("NA") in other classes Me ") function to transform the state--first determine whether this event can execute if self.fsm:canDoEvent (event) then Self.fsm:doEvent (event) end end return Hero

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.