So for my first practice project I have been making the first level in the arcade donkey kong. What I am stuck at now is making mario jump. I know there is an easy way of doing it in codea but I am not sure how. I kind of assume its has to do with the physics function (applyAngularImpulse but not sure).
supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)
function setup()
done=true
cb={x=60,y=300}
val=2
end
function jumpTween()
t1=tween(.5,cb,{y=400},tween.easing.cubicOut)
t2=tween(.5,cb,{y=300},tween.easing.cubicIn,tweenDone)
tween.sequence(t1,t2)
end
function duckTween()
t1=tween(.5,cb,{y=200},tween.easing.cubicOut)
t2=tween(.5,cb,{y=300},tween.easing.cubicIn,tweenDone)
tween.sequence(t1,t2)
end
function draw()
background(40, 40, 50)
fill(255)
text("swipe up to jump, swipe down to duck",WIDTH/2,HEIGHT-100)
rect(WIDTH/2,260,20,50)
if cb.x<50 or cb.x>WIDTH-50 then -- keep sprite on the screen
val=-val
end
cb.x=cb.x+val -- move sprite
sprite("Planet Cute:Character Boy",cb.x,cb.y) -- draw sprite
end
function tweenDone()
done=true -- tween is done, allow next jump or duck
end
function touched(t)
if t.state==MOVING and done then
done=false
if t.deltaY<0 then -- flick down
duckTween()
end
if t.deltaY>0 then -- flick up
jumpTween()
end
end
end
Thank’s Dave for that. I think that is a bit too complicated for what I am thinking. For example, I wrote a function using a rectangle:
function setup()
x,y=0,0
end
function draw()
rect(x,y,50,50)
x=x+1
y=-1/5x(x-50)
this kind of does what I want but its a bit awkward to control.
Edit: I am going to play around with your’s Dave since I think its going to work much better than mine.
@velkroe - I’m afraid you’ll find that programming any non trivial game is quite complicated. This means your code can get tangled very quickly.
One thing I suggest is to break your code into little functions, as Dave has done, eg duckTween. I would even do that for the code in the draw function, eg have a function DrawLevel which draws the current level, MovePlayer, which moves the player, and so on.
@Ignatz: Yeah this is my first real game type program that I have ever tried writing (did some smaller php type stuff but nothing very significant). I really decided to write this program to learn more about coding and needed a project. I have been trying to break things up as much as possible but my code is still pretty cumbersome. My Main section is a little over 300 lines and my Mario class is about 150 lines. My other classes are really small and probably could have been better thought out. As I have written ideas have come on how I could better organize it and make it smaller. I really should be using functions more.
One thing to look for is where you either find you have code that repeats (put it into a function) or a function (like draw) that is trying to do lots of things.
A good rule is to have each function “do one thing well”. The more you break up your code into functions, the easier it is to test each of them.
I usually have an extra Test tab on the right with its own setup and draw functions, and I test each function on its own, using that tab. This is possible since Codea doesn’t mind your having more than one setup function - it will use the right hand one. So when are running your app normally, you comment out the test tab, or slide it to the left of the other tabs.