Hello, I am making a game with a hero and an enemy, I want the hero to disappear when the enemy touches the hero and I want the game to restart. I am an absolute beginner. I am still learning so thanks!
@CalebAnder Are you going to use the physics engine for your objects, or are you going to calculate a collision on your own.
@CalebAnder Here’s an example using the physics engine. The enemy is dropping from the top and the hero is fixed. When the enemy hits the hero, the hero disappears and when the enemy falls off the bottom of the screen, the program restarts.
function setup()
a1=physics.body(CIRCLE,40)
a1.x=300
a1.y=HEIGHT-100
a2=physics.body(CIRCLE,40)
a2.x=300
a2.y=200
a2.type=STATIC
end
function draw()
background(40, 40, 50)
sprite("Tyrian Remastered:Evil Head",a1.x,a1.y)
if a2~=nil then
sprite("Platformer Art:Guy Standing",a2.x,a2.y)
end
if a1.y<-50 then
restart()
end
end
function collide(c)
if c.state==BEGAN then
a2:destroy()
a2=nil
end
end
@CalebAnder Here’s the above example not using the physics engine.
function setup()
show=true
a1x=300
a1y=HEIGHT-100
a2x=300
a2y=200
end
function draw()
background(40, 40, 50)
sprite("Tyrian Remastered:Evil Head",a1x,a1y)
if show then
sprite("Platformer Art:Guy Standing",a2x,a2y)
end
checkPosition()
end
function checkPosition()
v1=vec2(a1x,a1y)
d=v1:dist(vec2(a2x,a2y))
if d<75 then
show=false
end
a1y=a1y-2
if a1y<-50 then
restart()
end
end
Thanks Dave, helped a lot! I have just started using codea so I will ask for help a lot!