Adding Projectiles to the User

What I’m working on is a way to add projectiles to the user as they reach certain scores.

I’m using the Implementing a Health Status Bar in Corona SDK found here
http://omnigeek.robmiracle.com/2011/05/30/implementing-a-health-status-bar-in-corona-sdk/

So I have it all set up and it works fine, now what I did is they start off with 2 maxLives and as they reach scores a gamelife and the maxlives increase, but this way I get an error- corona sdk attempt to concatenate upvalue on orbshot **the line referenced is: local projectile = display.newImage( “”…orbshot…"" )

which I would think the orbdensity on the line above it would cause this error first.

function newProjectile()   
  
print("maxLives:"..maxLives)  
  
 if maxLives == 2 then  
 local orbshot = "imagesGame/cinderblock.png"  
 local orbdensity = 1  
 end  
 if maxLives == 3 then  
 local orbshot ="imagesGame/tire.png"   
 local orbdensity = 1.5   
  
 end  
 if maxLives == 4 then  
 local orbshot ="imagesGame/tirelooseicon.png"   
 local orbdensity = 1  
  
 end   
 if maxLives == 5 then  
 local orbshot ="imagesGame/tirelooseicon.png"   
 local orbdensity = 1  
  
 end   
  
 local projectileDensity = orbdensity  
 local projectile = display.newImage( ""..orbshot.."" )  
 game:insert( projectile )  
 physics.addBody( projectile, { density=projectileDensity, friction=0.7, bounce=0.2} )   
 orb=projectile  
  
 orb.myName = "orb";  
 orb.bodyType = "kinematic";  
 orb.x = 85;  
 orb.y = \_H ;  
 orb.linearDamping = 0.3 ;   
 orb.angularDamping = 0.8 ;   
 orb.isBullet = true   
 transition.to(orb, {time=600, y=\_H - 70, transition = easingx.easeOutElastic});  
  
 return orb;  
end  

If I remove these lines it works fine

 if maxLives == 2 then  
  
 end  

Except when I add to the maxlifes, it stays here and doesn’t go to the if maxlifes == 3 lines and so on.

Any ideas on why this is happening?
Dan [import]uid: 78446 topic_id: 20156 reply_id: 320156[/import]

Hey there,

First up - line 26 you don’t need to do lua[/lua] - you can just do lua[/lua]

For your issue, I am guessing higher up in your code you have something like;

[lua]local orbshot[/lua] ?

Try making it;

[lua]local orbshot = “imagesGame/cinderblock.png”[/lua]

Then run it and tell me how it goes.

Peach :slight_smile: [import]uid: 52491 topic_id: 20156 reply_id: 78889[/import]

Thanks Peach, moving it up did work.

Now I have a hard question for anyone who may have done this or somthing similiar, that may not be possible to do they way I doing this.

Right now my first two projectiles and icons are a cinderblock, when a player reaches a certain score I would like to add another projectile, right now the way this is set up it adds another cinderblock.

My problem is when I shoot a projectile it removes one from the gameLives and when it checks the score and possibly adds 1, it goes back up to 2 gameLives (at the most) which is a cinderblock.

Any idea how I can add a new projectile and it’s icon that’s not a cinderblock after 1 or 2 of them has been shot?

 local gameLives = 2  
 local maxLives = 2   
 local orbshot = "imagesGame/cinderblock.png"  
 local orbdensity = 1  
 local orbmultiplier = 9  
  
 if maxLives == 3 then  
 local orbshot ="imagesGame/tire.png"   
 local orbdensity = 1.5   
 local orbmultiplier = 14  
  
 end  
 if maxLives == 4 then  
 local orbshot ="imagesGame/tirelooseicon.png"   
 local orbdensity = 1  
 local orbmultiplier = 11  
 end   
 if maxLives == 5 then  
 local orbshot ="imagesGame/tirelooseicon.png"   
 local orbdensity = 1  
 local orbmultiplier = 11  
 end   
 local lifeIcons = {}   
   
 lifeIcons[1] = display.newImage("imagesGame/cinderblock.png")   
 lifeIcons[2] = display.newImage("imagesGame/cinderblock.png")   
 if maxLives == 3 then  
 lifeIcons[3] = display.newImage("imagesGame/tire.png")   
 end  
 if maxLives == 4 then  
 lifeIcons[4] = display.newImage("imagesGame/tirelooseicon.png")  
 end   
 if maxLives == 5 then  
 lifeIcons[5] = display.newImage("imagesGame/tirelooseicon.png")   
 end   
   
 local i   
  
 for i = 1, maxLives do   
 lifeIcons[i].x = screenW \* .01 + (lifeIcons[i].contentWidth \* (i -1) +10)   
 lifeIcons[i].y = screenH \*.04   
 lifeIcons[gameLives].isVisible = false   
 hudGroup:insert(lifeIcons[i])   
 end   
 lifeIcons[gameLives].isVisible = true   
  
-----FURTHER DOWN Aftercollision it checks score  
 local function checkscore()  
 if score \>= 500 and score \<= 700 and setorb ==0 then   
 scoretext()   
 gameLives = gameLives + 1  
 maxLives = maxLives + 1  
 setorb =1  
 if gameLives \> maxLives then  
 maxLives = gameLives  
 end  
 end   
 end  

Thanks
Dan [import]uid: 78446 topic_id: 20156 reply_id: 79068[/import]

Could you use a separate variable, like countShots = 0 to track this, then when it = 3 you switch from the cinderblock to another image?

Does that make sense?

Peach :slight_smile: [import]uid: 52491 topic_id: 20156 reply_id: 79091[/import]

Thanks I got it to work, I used setorb == 1 to keep track, but I also had to use the following to keep track if it was the first or second shot because if not, it would change my second shot from the cinderblock to a tire if the points where reached on the first shot. I need the tire to be the 3rd projectile.

 if setorb == 1 and maxLives == 3 and gameLives ~= 1 then  
  

My problem now is the icons. It doesn’t add an icon for it.
I define the icons-

 local lifeIcons = {}   
 lifeIcons[1] = display.newImage("cinderblock.png")   
 lifeIcons[2] = display.newImage("cinderblock.png")   
   
   
 local i   
  
 for i = 1, maxLives do   
 lifeIcons[i].x = screenW \* .01 + (lifeIcons[i].contentWidth \* (i -1) +10)   
 lifeIcons[i].y = screenH \*.04   
 lifeIcons[gameLives].isVisible = false   
 hudGroup:insert(lifeIcons[i])   
 end   
 lifeIcons[gameLives].isVisible = true   

I tried to add it where it adds the extra projectile

 if score \>= 500 and score \<= 1000 and setorb ==0 then   
 scoretext()   
 gameLives = gameLives + 1  
 maxLives = maxLives + 1  
 setorb = 1  
 lifeIcons[3] = display.newImage("tire.png")   
 end   
  

It adds the icon image, but it is over my other icons and doesn’t remove once the projectile is used. A cinderbock icon is there and it removes once used.

Here is a test file to see how it works
http://dl.dropbox.com/u/41194248/test.zip

Dan [import]uid: 78446 topic_id: 20156 reply_id: 80876[/import]