#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Some nooby questions

Posts: 3

Hi, as my first game using Lua I am making a game where you are a rocket, and you try to dodge asteroids. I have a few questions.

1) I’ve written a program for the asteroid movement and speed, but I am wondering how to make it work for multiple asteroids at once. I’m using various variables, but other than duplicating it and renaming all the variables how can I make it work for multiple sprites at once?

2) I am having trouble making the program detect whether the rocket is touching the asteroid. Is there a basic way of doing this?

3) My brother is also having a go at Codea, and he wants to make buttons that move his character right and left. I’ve tried to work it out myself, but I’m quite confused about how.

So yeah. Apologies for our noobiness. Any help would be greatly appreciated (by both me and my brother) as we would love to be able to program apps ourselves

• edited August 4 Posts: 1,272

@Purplecreature - simple one first, search the for buttons. Set up two simple buttons - you can use the UI sprites in the resources library. Use touch to detect when each button is touched then move the sprite by changing the x and y values in the sprite command:

``````sprite(button_left, xposition, yposition, sprite width, spriteheight)

``````

If you search the forum, and the Wiki, you will be able to find many examples of using touch. Try @ignatz website (link in the Wiki) which covers many of the Codea features that you will need for this.

Question on the rocket - is this a 2D program - rocket and asteroid move in x and y coordinates onscreen? Or, is it 3D where the asteroids are flying towards you - very different approach.= to these.

On making multiple asteroids you need to set up an asteroid Class which contains the data needed to generate the asteroid - e.g. the x and y position and size of each asteroid. Then you an provide a loop to interogate each asteroid and move it (in small increments) in whatever direction you choose or a random one.

Hint - collision detection involves measuring the distance between the each asteroid and the rocket - compensation if needed for the size of rocket and asteroid.

I'll look for an example in the forum - hope that helps to get you started.

Edit: try looking at the following link - a simple asteroids game

https://pastebin.com/uTcXchab

• Mod
edited August 5 Posts: 7,683

@Purplecreature Here's a simple example I threw together to give you an idea of things to try. Move your finger anywhere on the screen to move the ship up/down/right/left. Avoid the asteroids. Each asteroid that goes off the screen bottom increments the count at the top. Each asteroid hit decrements the ship count at the upper left. When over, press the restart icon to play again. Hold the iPad in portrait mode. You can add sound or other things to expand what's happening. If you have questions, just ask.

``````displayMode(FULLSCREEN)

function setup()
fontSize(40)
asteroids=30
past=0
ships=5
dx,dy=WIDTH/2,200
ast={}
for z=1,asteroids do
end
end

function draw()
background(0, 203, 255, 255)
fill(255,0,0)
text(past,WIDTH/2,HEIGHT-50)
if ships==0 then
text("Press restart icon to play again.",WIDTH/2,HEIGHT/2)
return
end
for z=1,#ast do
sprite("Space Art:Asteroid Small",ast[z].x,ast[z].y)
ast[z].y=ast[z].y-ast[z].z
end
chkPosition()
sprite("Tyrian Remastered:Plane Boss",dx,dy,-100)
chkCollision()
for z=1,ships do
sprite("Tyrian Remastered:Plane Boss",40*z,HEIGHT-30,30)
end
end

function chkPosition()
for z=#ast,1,-1 do
if ast[z].y<-50 then
table.remove(ast,z)
past=past+1
end
end
end

table.insert(ast,vec3(math.random(10,WIDTH-10),
math.random(HEIGHT+30,HEIGHT+500),math.random(2,5)))
end

function chkCollision()
for z=#ast,1,-1 do
v1=vec2(dx,dy)
d=v1:dist(vec2(ast[z].x,ast[z].y))
if d<65 then
table.remove(ast,z)
ships=ships-1
end
end
end

function touched(t)
if t.state==MOVING then
dx=dx+t.deltaX
dy=dy+t.deltaY
end
end
``````
• Posts: 3

@Bri_G @dave1707 Thanks so much! I’ve used both codes and now my game is functional!

Just two more things - Firstly, I’m probably just being really dumb, but how exactly do you make a button with touch commands? And secondly, is there an easy way to make the game stop when you die?

• Posts: 1,272
@Purplecreature - the button is actually a sprite - any sprite. The UI resources includes many with Codea. The trick is to check in the area where the button is, that's x and y coordinates to ensure you have touched the screen there, either when a touch has been initiated (BEGAN) or stopped (ENDED). The MOVING option is generally for dragging sprites.

Try :
~~~

if lives > 0 then
play()
else
demo()
end

~~~
• Mod
Posts: 7,683

@Purplecreature Here's a simple 2 button example. Tap the left of right button to move the sprite left or right. Or just slide your finger from button to button. Normally a class would be used to create buttons, but since I don't know if you know how to use classes, I just did this.

``````displayMode(FULLSCREEN)

function setup()
rectMode(CENTER)
b1=vec4(WIDTH/2-50,200,100,50)
b2=vec4(WIDTH/2+50,200,100,50)
sx,sy=WIDTH/2,HEIGHT/2  -- sprite x,y
xs=0    -- x speed
end

function draw()
background(0)
fill(255)
rect(b1.x,b1.y,b1.z,b1.w)   -- button 1
rect(b2.x,b2.y,b2.z,b2.w)   -- button 2
fill(255,0,0)
text("Left",b1.x,b1.y)
text("Right",b2.x,b2.y)
sprite("Planet Cute:Star",sx,sy)
sx=sx+xs
end

function touched(t)
if t.state==BEGAN or t.state==MOVING then
xs=0
if t.x>b1.x-b1.z/2 and t.x<b1.x+b1.z/2 and
t.y>b1.y-b1.w/2 and t.y<b1.y+b1.z/2 then
xs=-1
end
if t.x>b2.x-b2.z/2 and t.x<b2.x+b2.z/2 and
t.y>b2.y-b2.w/2 and t.y<b2.y+b1.z/2 then
xs=1
end
elseif t.state==ENDED then
xs=0
end
end
``````
• Mod
Posts: 7,683

@Purplecreature Here's an example that uses a button class just in case you're interested. I added up/down buttons.

``````displayMode(FULLSCREEN)

function setup()
rectMode(CENTER)
bTab={}
table.insert(bTab,button(WIDTH/2-70,150,100,50,-1,0,"Left"))
table.insert(bTab,button(WIDTH/2+70,150,100,50,1,0,"Right"))
table.insert(bTab,button(WIDTH/2,200,100,50,0,1,"Up"))
table.insert(bTab,button(WIDTH/2,100,100,50,0,-1,"Down"))
sx,sy=WIDTH/2,HEIGHT/2  -- sprite x,y
end

function draw()
background(0)
sprite("Planet Cute:Star",sx,sy)
for a,b in pairs(bTab) do
b:draw()
end
sx=sx+button.xs
sy=sy+button.ys
end

function touched(t)
button.xs,button.ys=0,0
for a,b in pairs(bTab) do
b:touched(t)
end
end

button=class()

button.xs=0
button.ys=0

function button:init(x,y,w,h,xs,ys,txt)
self.x=x
self.y=y
self.w=w
self.h=h
self.xs=xs
self.ys=ys
self.text=txt
end

function button:draw()
fill(255)
rect(self.x,self.y,self.w,self.h)
fill(255,0,0)
text(self.text,self.x,self.y)
end

function button:touched(t)
if t.state==BEGAN or t.state==MOVING then
if t.x>self.x-self.w/2 and t.x<self.x+self.w/2 and
t.y>self.y-self.h/2 and t.y<self.y+self.h/2 then
button.xs,button.ys=self.xs,self.ys
end
end
end
``````
• Posts: 3

@dave1707 @Bri_G Thanks again! Very useful!