side scroller
Flyingcursor

Hello again,

Here is another question.   I'm working on a simple side scroller.  A vehicle moving down a road.  I have a vehicle which basically sits in one place.  

I also have 2 identical road sprites, each road sprite is the same width as the scene.   Road1 starts at (0,199).  Road2 starts at (1024,199)

When user clicks right arrow, road1 and road2 moveTo(-1024,199, speed).  When the user releases the button, movement stops.

Currently it works fine and the road scrolls until it reaches it's moveTo() destination.

What I want is to reposition the road sprite to (1024, 199) so that can start moving again, making the road appear endless.

The problem is, whenever I check to see if the road has reached x=1024, I get an error saying that "a sprite has invalid coordinates".

Here is the code on my button "onMousedown"

var rd1 = wade.getSceneObject('road1');
var rd2 = wade.getSceneObject('road2');
var car = wade.getSceneObject('plyrVehicle');
var y1 = rd1.getPosition().y;
var y2 = rd2.getPosition().y;
var tree_y = trees.getPosition().y;

var dx = 1;
var curSpeed = car.curSpeed;
while (car.curSpeed < car.maxSpeed) {
    car.curSpeed = car.curSpeed + dx;
    rd1.moveTo(-1024,y1,car.curSpeed);
    rd2.moveTo(-1024,y2,car.curSpeed);

}

I tried adding an IF() within the while loop but I got the error.   I tried adding the code to the moveComplete event but obviously that doesn't work because it fires when I release the button, plus I still get the error.

I also tried to substitute moveTo(x,y,s) by simply incrementing the x position of the road while the button is down.  Again, the same error.

I believe I am missing the concept behind side scrolling with endless backgrounds.   Any suggestions will be greatly appreciated.

Thank you.

 

All 4 Comments
Flyingcursor

see below

Flyingcursor

Ugh.  Still having the same problem.   

Now, when the user clicks the arrow, set the velocity of the road object.   In the object's onUpdate() I use setVelocity to move it.

Everything works so far.

Then I check to see if the road has moved off the screen and reposition the road with x=scene width.   Again, I get the  error:

Warning: Some sprites have invalid coordinates, it isn't possible to render this frame.

I don't understand why.  I have two road sprites.  The first starts at 0,0 and the second at (screenWidth,0).  I'm unable to determine which sprite is causing the problem.

Here is the code in the road onUpdate() function.

this.setVelocity(wade.getSceneObject('vehicle').curSpeed,0);

if(this.getPosition().x <= -wade.getScreenWidth()) {
    this.setPosition(wade.getScreenWidth,0);
}

 

Gio

You are missing brackets after wade.getScreenWidth. It should be wade.getScreenWidth().

Saying that, you probably want to do:

this.setPosition((wade.getScreenWidth() + this.getSprite().getSize().x) / 2, 0);

 

Flyingcursor

Thank you Gio.   After I posted the above, I discovered my missing brackets.   

I finally got it working with this code:

var x1 = wade.getScreenWidth();
var y1 = this.getPosition().y;
var vehicle = wade.getSceneObject('vehicle');

this.setVelocity(vehicle.curSpeed,0);

if(this.getPosition().x <= -x1) {
    
    this.setPosition({x:x1,y:y1});
}

For some reason, once I passed an object to setPosition() everything fell into place.   But now I'm going to give your code a try.

Thank you again.   

Post a reply
Add Attachment
Submit Reply
Login to Reply