I have given piece of code for a game that is to be played in landscape left mode. first piece of code is executed as soon as the player enters the frame and this results in motion of player along an ellipse. the code inside first else is executed when user touches the screen and at this moment I want the player to jump, and code inside second jump is executed when touch ends that is player gets back to initial position, please help me what shall be the code to make the player jump while in elliptical motion
if playertouch == false then
if playertouchend == false then
x = 215 + math.cos(angle)*195
y = 130 + math.sin(angle)*115
else
--code to jump here
end
else
--code to get back down
end
Comments
Try this Code:(just copy it to main.lua)
-- You can jump by holding touch and back by release! (Sorry for my English )
application:setOrientation("landscapeLeft") -- good in 480*800
local xc = 400 -- X center of Ellipse
local yc = 200 -- Y center of Ellipse
local a = 400 -- big radius
local b = 100 -- small radius
local speed = 0.5 -- speed of rotating
local jump = 0
local maxjump = 50 -- can jump THIS pixel above
local jumpspeed = 1 -- speed of jumping
local theta = 0
local degree = math.pi / 180
local sinus = math.sin
local cosinus = math.cos
local sprite = gideros.class(Sprite)
function sprite:init()
self.jump = false
local mesh = Mesh.new()
mesh:setVertex(1,0,0)
mesh:setVertex(2,10,0)
mesh:setVertex(3,10,30)
mesh:setVertex(4,0,30)
mesh:setIndices(1,1,2,2,3,3,4,1,5,3,6,4)
mesh:setColorTransform(0.9,0,0,1)
self:addChild(mesh)
self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame,self)
self:addEventListener(Event.TOUCHES_BEGIN, self.onTouchesBegin, self)
self:addEventListener(Event.TOUCHES_END, self.onTouchesEnd, self)
end
function sprite:onEnterFrame()
if theta > 3600 then
theta = 3600 - theta
end
if self.jump and jump < maxjump then
jump = jump + jumpspeed
elseif jump > 0 then
jump = jump - jumpspeed
end
local angle = theta*degree
local x = xc + a * cosinus(angle)
local y = yc + b * sinus(angle) - jump
self:setPosition(x,y)
theta = theta + speed
end
function sprite:onTouchesBegin()
self.jump = true
end
function sprite:onTouchesEnd()
self.jump = false
end
mysprite = sprite.new()
stage:addChild(mysprite)
your body can jump in y axis (like most platform games)
this is what I am using for its motion in ellipse
x = 215 + math.cos(angle)*195
y = 130 + math.sin(angle)*115
Now, what shall i do to make the body jump while it is in motion along the ellipse, I want it to jump in the constraints of the ellipse.
Any idea, please?
you can change my code by this order:
local jump = 1
local maxjump = 1.2 -- can jump THIS pixel above
local jumpspeed = 0.02 -- speed of jumping
local x = xc + a * cosinus(angle)* jump
local y = yc + b * sinus(angle) *jump
elseif jump > 0 then
must be changed by:
elseif jump > 1 then
if playertouch then
speed = 10
angle2 = 30
speedx = math.cos(20)
speedy = math.sin(130)
velocityx = speed*speedx
velocityy = speed*speedy
for i = 1, 1 do
x = x + velocityx
y = y + velocityy
self: setScaleY(1.8)
end
elseif playertouch == false then
for i = 1,1 do
x = x - velocityx
y = y - velocityy
x = 215 + math.cos(angle)*195
y = 130 + math.sin(angle)*115
self:setScaleY(1)
end
else
x = 215 + math.cos(angle)*195
y = 130 + math.sin(angle)*115
end