How to create coins in side scroller game?

Hi guys,

how to create coins in side scroller game? It will have enterFrame and collision events?

Each coin should be a static physics object (if you coins are not effected by gravity).

Inside the players collision function you check if you hit a coin. If you did, remove it and add one to your coin counter.

What I need to do is

  1. Coins will be created with timer.

  2. Then the coins will be moved from right to left side because it is in the side scroller game, the things are moving from right to left.

  3. Then there are 2 functions, if the coins are in -50 position, it will be self-removed.

  4. If the player and coins are collided, then coins will be self-removed as well. 

Currently this is my code.
 

    xxx = display.newImage("img/xxx.png")     xxx.anchorX = 0     xxx.anchorY = 0     xxx.x = math.random(800, 1000)     xxx.y = math.random(80, 470)     xxx.myName = "xxx"     xxx.speed = 3     physics.addBody(xxx, "static")

local function xxxMoves(self, event) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; if self.x == nil then self.x = 0 end &nbsp; if self.x \< - 50 then &nbsp; &nbsp; self.x = math.random(800, 1000) &nbsp; &nbsp; self.y = math.random(80, 470) &nbsp; else &nbsp; &nbsp; self.x = self.x - self.speed &nbsp; end end

xxx.enterFrame = xxxMoves &nbsp; &nbsp; &nbsp; Runtime:addEventListener("enterFrame", xxx) // This is inside collicision function if (self.myName == "xxxer" and event.other.myName == "xxx") then&nbsp; &nbsp; &nbsp; &nbsp; Runtime:removeEventListener("enterFrame", xxx) &nbsp; &nbsp; &nbsp; event.other:removeSelf() &nbsp; &nbsp; &nbsp; event.other = nil &nbsp; &nbsp; end &nbsp; &nbsp;

Currently, what I am facing the problem is inside the collision, my question is

  1. How to make a function with timer including self-moving from right to left?

  2. Then how I can self-remove them?

Thanks.

It would be easier to add all the coins (any anything else that scrolls - background, platforms etc) to a single display group. You then move that in an enterFrame listener, and move the player in the opposite direction. 

What I want to do is

  1. In every second, the function will create new coins.

  2. Each coin has 2 conditions, if the coin is at -50, it will self-removed by itself. Another condition is, if the player and coin are collided each other, the coin will be self-removed.

Thanks guys.

Any example codes to do it?

Also a trick would be.
Do a counter. like levelcount = levelcount +1 every loop

And then insert coins on specific time stamps.
Remove the coins while collision or if out of screen.

@knight.royder, can you show me an example to do it? I don’t get what you mean now. I am sorry because I am still a novice :frowning:

So i would mention to do some more general tutorials.
Did you check to corona sdk tutorials on your disk?
This would be helpful.

Without your code it is quit difficult to get what you want to reach…
 

Each coin should be a static physics object (if you coins are not effected by gravity).

Inside the players collision function you check if you hit a coin. If you did, remove it and add one to your coin counter.

What I need to do is

  1. Coins will be created with timer.

  2. Then the coins will be moved from right to left side because it is in the side scroller game, the things are moving from right to left.

  3. Then there are 2 functions, if the coins are in -50 position, it will be self-removed.

  4. If the player and coins are collided, then coins will be self-removed as well. 

Currently this is my code.
 

&nbsp; &nbsp; xxx = display.newImage("img/xxx.png") &nbsp; &nbsp; xxx.anchorX = 0 &nbsp; &nbsp; xxx.anchorY = 0 &nbsp; &nbsp; xxx.x = math.random(800, 1000) &nbsp; &nbsp; xxx.y = math.random(80, 470) &nbsp; &nbsp; xxx.myName = "xxx" &nbsp; &nbsp; xxx.speed = 3 &nbsp; &nbsp; physics.addBody(xxx, "static")

local function xxxMoves(self, event) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; if self.x == nil then self.x = 0 end &nbsp; if self.x \< - 50 then &nbsp; &nbsp; self.x = math.random(800, 1000) &nbsp; &nbsp; self.y = math.random(80, 470) &nbsp; else &nbsp; &nbsp; self.x = self.x - self.speed &nbsp; end end

xxx.enterFrame = xxxMoves &nbsp; &nbsp; &nbsp; Runtime:addEventListener("enterFrame", xxx) // This is inside collicision function if (self.myName == "xxxer" and event.other.myName == "xxx") then&nbsp; &nbsp; &nbsp; &nbsp; Runtime:removeEventListener("enterFrame", xxx) &nbsp; &nbsp; &nbsp; event.other:removeSelf() &nbsp; &nbsp; &nbsp; event.other = nil &nbsp; &nbsp; end &nbsp; &nbsp;

Currently, what I am facing the problem is inside the collision, my question is

  1. How to make a function with timer including self-moving from right to left?

  2. Then how I can self-remove them?

Thanks.

It would be easier to add all the coins (any anything else that scrolls - background, platforms etc) to a single display group. You then move that in an enterFrame listener, and move the player in the opposite direction. 

What I want to do is

  1. In every second, the function will create new coins.

  2. Each coin has 2 conditions, if the coin is at -50, it will self-removed by itself. Another condition is, if the player and coin are collided each other, the coin will be self-removed.

Thanks guys.

Any example codes to do it?

Also a trick would be.
Do a counter. like levelcount = levelcount +1 every loop

And then insert coins on specific time stamps.
Remove the coins while collision or if out of screen.

@knight.royder, can you show me an example to do it? I don’t get what you mean now. I am sorry because I am still a novice :frowning:

So i would mention to do some more general tutorials.
Did you check to corona sdk tutorials on your disk?
This would be helpful.

Without your code it is quit difficult to get what you want to reach…