2 | function MainGameScene:switchTrace(car) |
4 | local circleCenter1,circleCenter2,point1,point2 = self:isSwitch(car) |
7 | local tm1,tm2,tm3,tm4,degree1,degree2 = self:setParam(car,circleCenter1,point1,point2) |
10 | self:switch(car,tm1,tm2,tm3,tm4,degree1,degree2,circleCenter1,circleCenter2,point1,point2) |
14 | function MainGameScene:isSwitch(car) |
18 | local circleCenter1 = self.track_size.height*0.445 |
19 | local circleCenter2 = -circleCenter1 |
20 | local point1 = cc.p(self.track_size.width*0.245,self.car_bottomy) |
21 | local point2 = cc.p(self.track_size.width*0.745,self.car_topy) |
25 | if car:getTag() == 1 then |
26 | switch = self.red_switch |
27 | self.red_switch = not switch |
31 | if switch == false then |
33 | circleCenter1 = self.track_size.height*0.335 |
34 | circleCenter2 = -circleCenter1 |
35 | point1 = cc.p(self.track_size.width*0.245,self.track_size.height*0.171) |
36 | point2 = cc.p(self.track_size.width*0.745,self.track_size.height*0.844) |
40 | if car:getTag() == 1 then |
42 | local x = car:getPositionX() |
44 | if x < self.track_size.width*0.245 then |
48 | local circleCenter = cc.pAdd(point1,{x=0,y=circleCenter1}) |
50 | radians = math.acos ((point1.x-x)/self._radius) |
52 | if car:getPositionY() > circleCenter.y then |
54 | self._degree = 90- math.deg (radians) |
56 | car:setPosition({x=circleCenter.x-circleCenter1* math.cos (radians),y=circleCenter.y+circleCenter1* math.sin (radians)}) |
58 | self._degree = 90+ math.deg (radians) |
60 | car:setPosition({x=circleCenter.x-circleCenter1* math.cos (radians),y=circleCenter.y-circleCenter1* math.sin (radians)}) |
63 | circleCenter1 = cc.pSub(circleCenter,{x=car:getPositionX(),y=car:getPositionY()}) |
64 | circleCenter2 = {x=0,y=circleCenter2} |
65 | elseif x > self.track_size.width*0.745 then |
69 | local circleCenter = cc.pAdd(point2,{x=0,y=circleCenter2}) |
71 | radians = math.acos ((x-point2.x)/self._radius) |
73 | if car:getPositionY() > circleCenter.y then |
75 | self._degree = 90+ math.deg (radians) |
77 | car:setPosition({x=circleCenter.x+circleCenter1* math.cos (radians),y=circleCenter.y+circleCenter1* math.sin (radians)}) |
79 | self._degree = 90- math.deg (radians) |
81 | car:setPosition({x=circleCenter.x+circleCenter1* math.cos (radians),y=circleCenter.y-circleCenter1* math.sin (radians)}) |
84 | circleCenter1 = {x=0,y=circleCenter1} |
85 | circleCenter2 = cc.pSub(circleCenter,{x=car:getPositionX(),y=car:getPositionY()}) |
86 | elseif car:getPositionY() > self.track_size.height*0.5 then |
87 | circleCenter1 = {x=0,y=circleCenter1} |
88 | circleCenter2 = {x=0,y=circleCenter2} |
89 | car:setPositionY(point2.y) |
91 | circleCenter1 = {x=0,y=circleCenter1} |
92 | circleCenter2 = {x=0,y=circleCenter2} |
93 | car:setPositionY(point1.y) |
98 | self._radius = math.sqrt ( math.pow (circleCenter1.x,2)+ math.pow (circleCenter1.y,2)) |
101 | return circleCenter1,circleCenter2,point1,point2 |
105 | function MainGameScene:setParam(car,circleCenter1,point1,point2) |
107 | local rate = math.pi *self.track_size.height*0.3 |
109 | local radius = self._radius |
111 | local tm1,tm2,tm3,tm4,degree1,degree2 |
113 | local x = car:getPositionX() |
114 | if x < self.track_size.width*0.245 then |
115 | tm1 = self._degree*2* math.pi /360*radius/rate |
116 | tm2 = (point2.x-point1.x)/rate |
117 | tm3 = math.pi *radius/rate |
118 | tm4 = (point2.x-point1.x)/rate |
119 | degree1 = self._degree |
121 | elseif x > self.track_size.width*0.745 then |
122 | tm1 = math.pi *radius/rate |
123 | tm2 = (point2.x-point1.x)/rate |
124 | tm3 = self._degree*2* math.pi /360*radius/rate |
125 | tm4 = (point2.x-point1.x)/rate |
127 | degree2 = self._degree |
128 | elseif car:getPositionY() > self.track_size.height*0.5 then |
129 | tm1 = math.pi *radius/rate |
130 | tm2 = (point2.x-x)/rate |
131 | tm3 = math.pi *radius/rate |
132 | tm4 = (point2.x-point1.x)/rate |
136 | tm1 = math.pi *radius/rate |
137 | tm2 = (point2.x-point1.x)/rate |
138 | tm3 = math.pi *radius/rate |
139 | tm4 = (x-point1.x)/rate |
144 | return tm1,tm2,tm3,tm4,degree1,degree2 |
148 | function MainGameScene:switch(car,tm1,tm2,tm3,tm4,degree1,degree2,circleCenter1,circleCenter2,point1,point2) |
153 | local circle1 = tt.CircleBy:create(tm1,circleCenter1,degree1) |
154 | local spawn1 = cc.Spawn:create(circle1,cc.RotateBy:create(tm1,degree1)) |
155 | spawn1 = cc.Sequence:create(spawn1,cc.RotateTo:create(0,180)) |
157 | local circle2 = tt.CircleBy:create(tm3,circleCenter2,degree2) |
158 | local spawn2 = cc.Spawn:create(circle2,cc.RotateBy:create(tm3,degree2)) |
159 | spawn2 = cc.Sequence:create(spawn2,cc.RotateTo:create(0,360)) |
161 | local move1 = cc.MoveTo:create(tm4,point1) |
163 | local move2 = cc.MoveTo:create(tm2,point2) |
165 | local callfunc = cc.CallFunc:create( function () |
166 | self:runRedCar(self.car_red) |
172 | local x = car:getPositionX() |
173 | if x <= self.track_size.width*0.245 then |
174 | sequence = {spawn1,move2,spawn2,move1,callfunc} |
175 | elseif x >= self.track_size.width*0.745 then |
176 | sequence = {spawn2,move1,spawn1,move2,callfunc} |
177 | elseif car:getPositionY() > self.track_size.height*0.5 then |
178 | sequence = {move2,spawn2,move1,spawn1,callfunc} |
180 | sequence = {move1,spawn1,move2,spawn2,callfunc} |
183 | local action_red = cc.Sequence:create(sequence) |
189 | if car:getTag() == 1 then |
190 | car:runAction(action_red) |
在switchTrace中将runRedCar()函数的工作拆分为三部分来完成,isSwitch函数是获得变轨以后动作的俩个圆心和俩个点,setParam就是用来设置执行动作的一些参数的,包括时间以及圆周运动的角度,最后的switch函数完成变轨动作,注意动作执行完毕以后调用了runRedCar函数,这样红色小车的整个变轨逻辑就完成了,最后看一下效果吧。