Any threads on how to make a score counter for every "dodge"

@davy1222 your code is so wasteful…  You really need to invest some time to understand arrays of objects.  It will make your code far more simple and readable.

For example, this code

local function move(event) bg2.x = bg2.x - scrollSpeed/2 bg3.x = bg3.x - scrollSpeed/2 bg4.x = bg4.x - scrollSpeed/2 bg5.x = bg5.x - scrollSpeed/2 bg6.x = bg6.x - scrollSpeed/2 bg7.x = bg7.x - scrollSpeed bg8.x = bg8.x - scrollSpeed bg9.x = bg9.x - scrollSpeed bg10.x = bg10.x - scrollSpeed bg11.x = bg11.x - scrollSpeed if(-bg2.x + bg2.contentWidth) \> 1080 then bg2:translate(2000,0) end if(-bg3.x + bg3.contentWidth) \> 1080 then bg3:translate(2000,0) end if(-bg4.x + bg4.contentWidth) \> 1080 then bg4:translate(2000,0) end if(-bg5.x + bg5.contentWidth) \> 1080 then bg5:translate(2000,0) end if(-bg6.x + bg6.contentWidth) \> 1080 then bg6:translate(2000,0) end if(-bg7.x + bg7.contentWidth) \> 1080 then bg7:translate(2000,0) end if(-bg8.x + bg8.contentWidth) \> 1080 then bg8:translate(2000,0) end if(-bg9.x + bg9.contentWidth) \> 1080 then bg9:translate(2000,0) end if(-bg10.x + bg10.contentWidth) \> 1080 then bg10:translate(2000,0) end if(-bg11.x + bg11.contentWidth) \> 1080 then bg11:translate(2000,0) end end

could be simplified to

local function move(event) for i = 1, 11 do if i \< 7 then gb[i].x = gb[i].x - scrollSpeed/2 else gb[i].x = gb[i].x - scrollSpeed end if(-bg[i].x + bg[i].contentWidth) \> 1080 then bg[i]:translate(2000,0) end end end

Yeah, there’s a lot to improve upon regarding the code.

As for SGS’s math solution, it is a simpler method and it can easily be modified to work with circles as well. If your game doesn’t require physics apart from the collision detection for the dodging itself, then opting to use a purely mathematical solution is better.

For the benefit of others reading this…

Also, there is not a use case (realistically) that needs more than a single enterFrame event.

your code works similarly

https://www.youtube.com/watch?v=TpnH_jmcpOM&feature=youtu.be

 

but this is closer to what I’m trying to achieve outside of the bar moving forward

https://www.youtube.com/watch?v=KmVkTCg30OM&feature=youtu.be

This happens when I use the code from the isSensor thread and set the linear velocity (1,0)

Yeaa I know my code is a bit messy, I was hoping it wouldn’t be too obvious but I got a little excited when I found this app and got right into making the game instead of looking into lua.

Also my code needs physics for the touch function otherwise the penguin stays stationary. 

Also I appreciate you guys’s help I know how frustrating it must be talking to someone who has no clue what they’re doing.

Alright, seems like I had a completely different idea of your game in my head. This is why visual aids are so great.

From what I can see, you could achieve all of that without the use of physics. Since those ice blocks aren’t perfect rectangles, you’d sacrifice a bit of accuracy in terms of collision detection, but your game would be less resource heavy and it would require less code as a result.

For scoring, you currently have a large rect that detects when those ice blocks reach the left side of the screen, but you shouldn’t use collision detection here. Just write an enterFrame function that loops through all ice blocks. If an ice block’s x location is to the left of the penguin and it hasn’t collided with the penguin, then obviously it was dodged. At this point, you’d add one point to the player (and you could stop tracking ice blocks that have yielded points already to make the function lighter). For detecting if an ice block collides with the penguin, you could use SGS’s function.

As for the penguin remaining stationary, from the looks of it, you could just add a few lines of code to the enterFrame function to drag the penguin down if the player isn’t touching the screen. If the player is touching the screen, then you’d move the penguin up.

Now, probably the reason as to why you need to move the scorebar in your code (as it is now), is if you move those iceblocks with transitions, then most likely the collisions don’t occur. What you could do, if you want to stick to physics, is to do something along the lines of:
 

local physics = require("physics") physics.setDrawMode( "hybrid" ) physics.start() local iceblock, iceblockTransition = {}, {} local function onLocalCollision( self, event ) if ( event.phase == "began" ) then if self.name == "scorebar" then print("Collision") end end end local scorebar = display.newRect(100,160,20,320) physics.addBody( scorebar, "dynamic", {density=1, isSensor=true} ) scorebar.collision = onLocalCollision scorebar.name = "scorebar" scorebar.gravityScale = 0 scorebar:addEventListener( "collision" ) scorebar:setFillColor(0,1,1) local function moveBlock() iceblock[#iceblock+1] = display.newRect(400,math.random(40,280),20,20) physics.addBody( iceblock[#iceblock], "kinematic" ) iceblock[#iceblock].collision = onLocalCollision iceblock[#iceblock]:addEventListener( "collision" ) iceblockTransition[#iceblockTransition+1] = transition.to( iceblock[#iceblock], {time=1500,x=0} ) end local iceblockTimer = timer.performWithDelay( 500, moveBlock, 0 )

But, once again, I would advice against the use of physics in this case as you don’t (at least yet) have anything that makes the use of physics compulsory.

Now, I hope that we’ve solved this issue, so that we can lay this thread to rest. :smiley: