Sprite position in middle of background

Hi there,

I am trying to figure out how to move the background when my character sprite moves. Basically I have implemented moving left, right buttons for the character sprite, However would love to go to the next level. I want the background to move when my character moves to the right or left. Essentially, would prefer keeping the character to the centre of the screen always. I remember doing this with Gamemeaker and it was not that difficult. I have looked around a bit for suitable code, cant find much. Can you please provide me the code/example so that I may be able to do the same with Corona sdk?

For those of you who are interested to know about my pet project, here is the screencast. Asset taken is for test purpose, I will take my own asset after I am done with the logic implementations.

Here you go the screencast link :slight_smile:

Link : http://www.screencast.com/t/njQN8pmelx

Basically I need the following help from other developers :

  • How to implement double run, when I tap twice fast on the left/right button the character will sprint faster than the usual pace

  • How to implement movement of background when the character moves?

I do have more questions, but these are my next tasks, so help would be much appreciated. Please let me know how I may improve on this demo :slight_smile:

Cheers,

Shahjada Tanvir

Well if you want to keep the character in the center of the screen don’t move the character at all, just move the background only and in the opposite direction. 

So instead of:

local function moveRight() thePlayer.x = thePlayer.x + 1 end

do this:

local function moveRight() theBackground.x = theBackground.x - 1 end

As for double-tap running, you just need to track when the first tap is made, and then the next time a tap is made check the time difference between them. If the time is short enough (i.e. it was a double tap), then increase the value of a “speed” variable:

local moveSpeed = 1 local lastTapTime = 0 local maxTimeForDoubleTap = 250 --need to do second tap within 250ms to count as a double tap local moveRight() theBackground.x = theBackground.x - moveSpeed end local function touchRight(event) --find out how long between last tap and this tap. if this is the very first tap then lastTapTime will be 0, so will count as a single tap local timeDiff = event.time - lastTapTime if timeDiff \<= maxTimeForDoubleTap then --if double tapped then set speed value to 2 moveSpeed = 2 else --else set it to 1 moveSpeed = 1 end --record the current time the tap occured, ready for next time lastTapTime = event.time --call the function to move right moveRight() end

That’s as simple as I can make it, hopefully that’s enough for you to start with.

Hello Alan,

Thanks a million for your help with the code. I implemented double tap run as you illustrated in the code.

Actually I was a bit stuck on this for a while and you solved the mystery :slight_smile: Thanks so much. 

Any feedback regarding the screencast link I gave? Your suggestions would be valuable for me. I am new

in Corona, but I am determined to complete a platformer game and release it. I stay up midnights and code

through the game…:slight_smile:

Hi Ahmed. 

Preview looks nice, there is still a lot to be done though!

Just one thing i noticed, when you attack, the sprisheet goes all over the place, try adjusting your anchor points (default is 0.5)

https://docs.coronalabs.com/api/type/DisplayObject/anchorX.html

https://docs.coronalabs.com/api/type/DisplayObject/anchorY.html

Hello Undecode,

Thanks a lot for your response. I shall fix the spritesheet issue as soon as possible and post a link of the updated gameplay :slight_smile:

Thanks once again for the links. I know I have a lot to do with the small platformer engine I am making. I want to fix all movement related issues first and implement the action sequences I have in mind.

I am currently having an issue with implementing double jump. I want to limit my character jump to maximum 2 and then fall to ground.

How can I implement this? Could you please provide me a sample code which can be used to emulate double jump?

Thanks,

Shahjada Md. Tanvir Ahmed

Well if you want to keep the character in the center of the screen don’t move the character at all, just move the background only and in the opposite direction. 

So instead of:

local function moveRight() thePlayer.x = thePlayer.x + 1 end

do this:

local function moveRight() theBackground.x = theBackground.x - 1 end

As for double-tap running, you just need to track when the first tap is made, and then the next time a tap is made check the time difference between them. If the time is short enough (i.e. it was a double tap), then increase the value of a “speed” variable:

local moveSpeed = 1 local lastTapTime = 0 local maxTimeForDoubleTap = 250 --need to do second tap within 250ms to count as a double tap local moveRight() theBackground.x = theBackground.x - moveSpeed end local function touchRight(event) --find out how long between last tap and this tap. if this is the very first tap then lastTapTime will be 0, so will count as a single tap local timeDiff = event.time - lastTapTime if timeDiff \<= maxTimeForDoubleTap then --if double tapped then set speed value to 2 moveSpeed = 2 else --else set it to 1 moveSpeed = 1 end --record the current time the tap occured, ready for next time lastTapTime = event.time --call the function to move right moveRight() end

That’s as simple as I can make it, hopefully that’s enough for you to start with.

Hello Alan,

Thanks a million for your help with the code. I implemented double tap run as you illustrated in the code.

Actually I was a bit stuck on this for a while and you solved the mystery :slight_smile: Thanks so much. 

Any feedback regarding the screencast link I gave? Your suggestions would be valuable for me. I am new

in Corona, but I am determined to complete a platformer game and release it. I stay up midnights and code

through the game…:slight_smile:

Hi Ahmed. 

Preview looks nice, there is still a lot to be done though!

Just one thing i noticed, when you attack, the sprisheet goes all over the place, try adjusting your anchor points (default is 0.5)

https://docs.coronalabs.com/api/type/DisplayObject/anchorX.html

https://docs.coronalabs.com/api/type/DisplayObject/anchorY.html

Hello Undecode,

Thanks a lot for your response. I shall fix the spritesheet issue as soon as possible and post a link of the updated gameplay :slight_smile:

Thanks once again for the links. I know I have a lot to do with the small platformer engine I am making. I want to fix all movement related issues first and implement the action sequences I have in mind.

I am currently having an issue with implementing double jump. I want to limit my character jump to maximum 2 and then fall to ground.

How can I implement this? Could you please provide me a sample code which can be used to emulate double jump?

Thanks,

Shahjada Md. Tanvir Ahmed