-SOLVED- Random Spawner?

I was wondering how I could make a method that spawns coins randomly off screen, and then propels them towards the viewing area randomly.

In a way, I need something similar to Factor Samurai where it picks a random number, then creates it, then propels it up on the screen. The only differences is that instead of multiple possibilities, I only need three, and I need them to spawn on the left and right sides of the screen.

Inside the method, I just need to create one of three objects on the left or right, shoot it onto the screen in a random direction, and add them to the physics.

Thanks for the help! [import]uid: 103624 topic_id: 17976 reply_id: 317976[/import]

You can easily spawn Objects outside of the screen.

  
local function createObject()  
 -- 0 will be left, 1 will be right  
 local right = math.random(0,1)  
  
 local obj = display.newImage("myImage.png")  
 if (right) then  
 obj.x = display.contentWidth+(obj.width/2)  
 else  
 obj.x = -(obj.width/2)  
 end  
 return obj  
end  

You could also give this object the physics properties and afterwards apply a linearImpulse to shoot it towards the screen :slight_smile: [import]uid: 13097 topic_id: 17976 reply_id: 68642[/import]

What would it look like with those pieces of code in? And, how often will I call it. [import]uid: 103624 topic_id: 17976 reply_id: 68643[/import]

How often you want those objects to spawn. It’s up to you!
you could use a timer (timer.performWithDelay).

For the other functions check out

Physic Bodies:
http://developer.anscamobile.com/reference/index/physicsaddbody

setLinearImpulse:
http://developer.anscamobile.com/reference/index/bodyapplylinearimpulse

Once you have a physic body, you can give it an impulse to “shoot” it :slight_smile:
You should put it in the function above where you can see if it’s positioned left or right ( to shoot it towards the center).

imho it’s the best to try stuff out, if you still happen to have problems, i’m willing to help you out :slight_smile: [import]uid: 13097 topic_id: 17976 reply_id: 68644[/import]

I made some progress, but I’m struggling with the setLinearImpulse. Also, it always seems to spawn on the right, or at least I can’t see them on the left.

Could you take a look?

http://dl.dropbox.com/u/27644193/Makin'%20Bank.zip [import]uid: 103624 topic_id: 17976 reply_id: 68649[/import]

I tried to check your code, but I could only see a white square.
then after switching to android i got something. but I can’t see the graphics for some reason - only white…
but okay. the first problem…

Change line 74 to

if (isRight == 1) then  

I thought if a value is 0, it would be false. that was wrong. so this should fix that problem :slight_smile:
now to the linear impulse:
It looks okay. I can’t test it since it’s all white for me :confused:
but you would just use a random positiv value to shoot it to the right side and a negative value to shoot it to the left!

[code]

function createProjectiles()
local projectile = display.newImage(“visuals/game_coinsilver.png”)
projectile:setReferencePoint(display.TopLeftReferencePoint);
local isRight = math.random(0, 1)
if (isRight == 1) then
projectile.x = 800
projectile.y = 216
physics.addBody(projectile)
projectile:applyLinearImpulse(-math.random(280, 350), 300)
else
projectile.x = -48
projectile.y = 216
physics.addBody(projectile)
projectile:applyLinearImpulse(math.random(280, 350), 300)
end
return projectile
end

[/code] [import]uid: 13097 topic_id: 17976 reply_id: 68651[/import]

Now they spawn on both sides, but they aren’t being shot inward.

Also, you aren’t seeing anything on iOS because the build.settings are only for Android.

[lua]function createProjectiles()
local projectile = display.newImage(“visuals/game_coinsilver.png”)
projectile:setReferencePoint(display.TopLeftReferencePoint);
local isRight = math.random(0, 1)
if (isRight == 1) then
projectile.x = 800
projectile.y = 216
physics.addBody(projectile)
projectile:applyLinearImpulse(-math.random(280, 350), 300)
else
projectile.x = -48
projectile.y = 216
physics.addBody(projectile)
projectile:applyLinearImpulse(math.random(280, 350), 300)
end
return projectile
end[/lua] [import]uid: 103624 topic_id: 17976 reply_id: 68657[/import]

White square is because the images aren’t the correct types - I mention this in the other thread you’ve got going, IKinx. [import]uid: 52491 topic_id: 17976 reply_id: 68708[/import]

I resaved the images to work next time. Anyways, my objects are being shot, but they’re behaving badly. :confused:

I want them to sort of be lobbed into the air and then fall down.

http://dl.dropbox.com/u/27644193/Makin'%20Bank.zip [import]uid: 103624 topic_id: 17976 reply_id: 68735[/import]

It’s a trial and error thingy.
Just play arounds with the values.
Try changing the y-value for the impulse to shoot it up high. And you might also change the objectes y-position to be more at the bottom.
It’s up to you!

I can tell you what functions etc to use, but you have to tweak it for your needs :slight_smile: [import]uid: 13097 topic_id: 17976 reply_id: 68736[/import]

I have the general direction, but it’s shooting out way too fast off of the screen. Is there something wrong with these lines?

[lua]–Left
projectile:applyLinearImpulse(300, -math.random(300, 600))
–Right
projectile:applyLinearImpulse(-300, -math.random(300, 600))[/lua]

And if not, what did I screw up on?

http://dl.dropbox.com/u/27644193/Makin'%20Bank.zip [import]uid: 103624 topic_id: 17976 reply_id: 68737[/import]

Your code is right, but you haven’t tweaked it.

The first problem is that you have used a top left referencePoint!
Without telling the linearImpulse where it should have it’s massCenter, you’ll have it where the referencePoint is! That’s why it looked jaggy!

The next thing: it’s way to strong. I was playing around with values and got a decent result!
Also you might want to change those projectiles to projectile.isSensor = true, otherwise they’ll collide with eachother!

Here’s the working code:

function createProjectiles() local chooseProjectile = math.random(0, 0) if (chooseProjectile == 0) then projectile = display.newImage("visuals/game\_coinsilver.png") elseif (chooseProjectile == 1) then projectile = display.newImage("visuals/game\_coingold.png") elseif (chooseProjectile == 2) then projectile = display.newImage("visuals/game\_rock.png") end --projectile:setReferencePoint(display.TopLeftReferencePoint) local chooseLocation = math.random(0, 1) if (chooseLocation == 0) then projectile.x = -48 projectile.y = 421 physics.addBody(projectile) projectile:applyLinearImpulse(.2, -math.random(3,5)/10,projectile.x,projectile.y) elseif (chooseLocation == 1) then projectile.x = 800 projectile.y = 421 physics.addBody(projectile) projectile:applyLinearImpulse(-.2, -math.random(3,5)/10,projectile.x,projectile.y) end return projectile end [import]uid: 13097 topic_id: 17976 reply_id: 68738[/import]

And to make your code a bit shorter and more effective:

function createProjectiles()  
 local projectileImages = {"coinsilver","coingold","rock"}  
 local random = math.random(1,#projectileImages)  
 projectile = display.newImage("visuals/game\_"..projectileImages[random]..".png")  
  
 physics.addBody(projectile)  
 projectile.isSensor = true  
 projectile.isFixedRotation = true  
  
 --\> 0 = left; 1 = right  
 local chooseLocation = math.random(0, 1)  
  
 projectile.y = 421  
 projectile.x = (-projectile.width/2)+chooseLocation \* (display.contentWidth+projectile.width)  
 projectile:applyLinearImpulse(.2-(chooseLocation\*.4), -math.random(3,5)/10,projectile.x,projectile.y)  
 print("Shooting a " .. projectileImages[random])  
 return projectile  
end  

Make sure the images exist!
The code is a bit more complex and has no if-conditionals and is calculating the correct positions and shoot-directions :slight_smile: [import]uid: 13097 topic_id: 17976 reply_id: 68739[/import]

How can I use that simplified code using these animated sprites? I tried to do it myself but failed. :frowning:
[lua] local projectilesheet = sprite.newSpriteSheet(“visuals/game_projectiles.png”, 48, 48)
local projectileset = sprite.newSpriteSet(projectilesheet, 1, 18)
sprite.add (projectileset, “coinsilver”, 1, 6, 250, 0)
sprite.add (projectileset, “coingold”, 7, 6, 250, 0)
sprite.add (projectileset, “rock”, 13, 6, 250, 0)
projectile = sprite.newSprite(projectileset)[/lua] [import]uid: 103624 topic_id: 17976 reply_id: 68743[/import]

Hm I don’t know. Haven’t been using spriteSheets yet, but a modified version of Jonathan Beebes BeebeGame Class!

I guess there’s a way to randomly use a sprite. I don’t know if you can give it physic properties though.

Maybe someone else can help you :confused: Sorry [import]uid: 13097 topic_id: 17976 reply_id: 68745[/import]

Does anyone know how to do it using the provided code above? [import]uid: 103624 topic_id: 17976 reply_id: 68749[/import]

Although everyone tries to offer as much support as they can in some cases you may be better off going with premium support. Link: http://www.anscamobile.com/corona/support/

It’s aimed at people who want code debugged or code written for them.

Peach :slight_smile: [import]uid: 52491 topic_id: 17976 reply_id: 68758[/import]

I did it! Now, I’m trying to get it so that the when the pig collides with the coins, it gets rid of the coins. I’m not sure why this doesn’t work. Any ideas?

[lua]function createPig()
local pigsheet = sprite.newSpriteSheet(“visuals/game_pig.png”, 96, 96)
local pigset = sprite.newSpriteSet(pigsheet, 1, 6)
sprite.add (pigset, “pigmleft”, 1, 3, 250, 0)
sprite.add (pigset, “pigmright”, 4, 3, 250, 0)
pig = sprite.newSprite(pigset)
pig:setReferencePoint(display.TopLeftReferencePoint);
pig.x = display.contentWidth / 2 - 48
pig.y = 349
function collectProjectile(event)
if(event.phase == “ended”) then
if(event.other.type == “coinsilver”) then
event.other:removeSelf()
print(“Silver Coin!”)
elseif(event.other.type == “coingold”) then
event.other.removeSelf()
print(“Gold Coin!”)
elseif(event.other.type == “rock”) then
event.other.removeSelf()
print(“Rock!”)
end
end
end
pig:addEventListener(“collision”, collectProjectile)
end
function createProjectiles()
local projectilesheet = sprite.newSpriteSheet(“visuals/game_projectiles.png”, 48, 48)
local projectileset = sprite.newSpriteSet(projectilesheet, 1, 18)
sprite.add (projectileset, “coinsilver”, 1, 6, 250, 0)
sprite.add (projectileset, “coingold”, 7, 6, 250, 0)
sprite.add (projectileset, “rock”, 13, 6, 250, 0)
projectile = sprite.newSprite(projectileset)
physics.addBody(projectile)
projectile.isSensor = true
projectile.isFixedRotation = true
local chooseProjectile = math.random(0, 2)
if (chooseProjectile == 0) then
projectile.type = “coinsilver”
projectile:prepare(“coinsilver”)
projectile:play(“coinsilver”)
elseif (chooseProjectile == 1) then
projectile.type = “coingold”
projectile:prepare(“coingold”)
projectile:play(“coingold”)
elseif (chooseProjectile == 2) then
projectile.type = “rock”
projectile:prepare(“rock”)
projectile:play(“rock”)
end
local chooseLocation = math.random(0, 1)
projectile.y = 421
projectile.x = -24 + chooseLocation * 848
projectile:applyLinearImpulse(0.2 - (chooseLocation * 0.4), -math.random(3, 4) / 10, projectile.x, projectile.y)
leftButton:toFront()
rightButton:toFront()
return projectile
end[/lua] [import]uid: 103624 topic_id: 17976 reply_id: 68777[/import]

Your collision function isn’t quite right. Take a look at the Collision Detection part of Corona For Newbies Part 4 on http://Techority.com - that should help.

Peach :slight_smile: [import]uid: 52491 topic_id: 17976 reply_id: 68860[/import]

I made a lot of progress just now, but my projectiles aren’t spawning correctly.

  1. You can see them when they’re off the screen.
  2. The right side spawns inside the screen while the left doesn’t. I want both to spawn outside.

[lua] local chooseLocation = math.random(0, 1)
projectile.y = 300
projectile.x = -24 + chooseLocation * 828
projectile:applyLinearImpulse(0.2 - (chooseLocation * 0.4), -math.random(0.75, 3.475) / 10, projectile.x, projectile.y)[/lua]

And a picture: http://dl.dropbox.com/u/27644193/Makin'%20Bank.png

Thanks for the help! [import]uid: 103624 topic_id: 17976 reply_id: 68933[/import]