Scrolling Background Error

Subscribe to Scrolling Background Error 7 posts

avatar for SSDRWH0 SSDRWH0 27 posts
Flag Post

I’ve been making an RTS game (but dealing with small problems first), and I’ve been looking at forums to solve things that I have some trouble with, until now.

Whenever I get my player character to move towards at least half way before switching to camera mode, the background will scroll out of range for some reason (that only occurs on the left hand side, but I can simply re-enter the background).

This also occured when I tried to implement a different character movement where acceleration and deceleration is involved.

here are my files if anyone wants to check:

www.mediafire.com/?5v8gya5u93h7nr2


I’ll just post the background code 1st. But the reason why I posted that file is that I’m not sure if the orblem is only in the background class.
[code block]



Third times the charm! (I wish I can edit my post in these forums… Would’ve saved me a lot of posts…)

package{

	import flash.display.MovieClip;
	import flash.events.Event;
	
	public class Background extends MovieClip{
		
		public function Background(){
			
		}

		public function Moving(xSpeed:Number, xDistance:Number){
			if (xSpeed == -ControlledUnit.speedFactor){
				if (Gameplay2.controlledUnit.x <= (Gameplay2.gameWidth/2)) {
            		if (Math.abs(this.x) > 0) {
						this.x -= xDistance*xSpeed;
						if (PixelPerfectCollisionDetection.isColliding(Gameplay2.targetDummy, Gameplay2.controlledUnit, this, true)) 
						{
							if (Gameplay2.controlledUnit.y >= (Gameplay2.targetDummy.y - 5) && Gameplay2.controlledUnit.y <= (Gameplay2.targetDummy.y + 5)){
								this.x += xDistance*xSpeed;
								Gameplay2.targetDummy.x += xDistance*xSpeed;
								Gameplay2.enemy.x += xDistance*xSpeed;
								Gameplay2.mainBase.x += xDistance*xSpeed;
							}
						}
                		Gameplay2.controlledUnit.x -= xDistance*xSpeed;
						Gameplay2.targetDummy.x -= xDistance*xSpeed;
						Gameplay2.enemy.x -= xDistance*xSpeed;
						Gameplay2.mainBase.x -= xDistance*xSpeed;
					}
				}
			}else{
				if (Gameplay2.controlledUnit.x >= (Gameplay2.gameWidth/2)) {
            		if (Math.abs(this.x) < this.width - Gameplay2.gameWidth-4) {
						this.x -= xDistance*xSpeed;
						if (PixelPerfectCollisionDetection.isColliding(Gameplay2.targetDummy, Gameplay2.controlledUnit, this, true)) 
						{
							if (Gameplay2.controlledUnit.y >= (Gameplay2.targetDummy.y - 5) && Gameplay2.controlledUnit.y <= (Gameplay2.targetDummy.y + 5)){
								this.x += xDistance*xSpeed;
								Gameplay2.targetDummy.x += xDistance*xSpeed;
								Gameplay2.enemy.x += xDistance*xSpeed;
								Gameplay2.mainBase.x += xDistance*xSpeed;
							}
						}
                		Gameplay2.controlledUnit.x -= xDistance*xSpeed;
						Gameplay2.targetDummy.x -= xDistance*xSpeed;
						Gameplay2.enemy.x -= xDistance*xSpeed;
						Gameplay2.mainBase.x -= xDistance*xSpeed;
					}
				}
			}
		}
		public function cameraMoving(xSpeed:Number, xDistance:Number){
			if (xSpeed == -ControlledUnit.speedFactor){
				trace("1) " + this.x);
				if (Math.abs(this.x) > 0) {
					this.x -= xDistance*xSpeed;
                	Gameplay2.controlledUnit.x -= xDistance*xSpeed;
					Gameplay2.targetDummy.x -= xDistance*xSpeed;
					Gameplay2.enemy.x -= xDistance*xSpeed;
					Gameplay2.mainBase.x -= xDistance*xSpeed;
					trace("2) " + this.x);
				}
			}else{
            	if (Math.abs(this.x) < this.width - Gameplay2.gameWidth-4) {
					this.x -= xDistance*xSpeed;
                	Gameplay2.controlledUnit.x -= xDistance*xSpeed;
					Gameplay2.targetDummy.x -= xDistance*xSpeed;
					Gameplay2.enemy.x -= xDistance*xSpeed;
					Gameplay2.mainBase.x -= xDistance*xSpeed;
				}
			}
		}
	}
}

And this is the actual line of code I suspect has some problems (I think)

public function cameraMoving(xSpeed:Number, xDistance:Number){
			if (xSpeed == -ControlledUnit.speedFactor){
				if (Math.abs(this.x) > 0) {
					this.x -= xDistance*xSpeed;
                	Gameplay2.controlledUnit.x -= xDistance*xSpeed;
					Gameplay2.targetDummy.x -= xDistance*xSpeed;
					Gameplay2.enemy.x -= xDistance*xSpeed;
					Gameplay2.mainBase.x -= xDistance*xSpeed;
				}else{
					trace("hello!");
				}
			}else{
 
avatar for Senekis93 Senekis93 4090 posts
Flag Post

Post the relevant code here. Not many will download that.

 
avatar for JamesObscura JamesObscura 250 posts
Flag Post

That if statement looks like something that you’d see to prevent the background from scrolling, so you’re on the right track. But it’s still impossible to tell whats going on. I don’t even know what xDistance*xSpeed is trying to do. According to the distance formula, Distance * Speed = Time * Speed^2; So that’s a bit odd. It’s also hard to tell what your problem even is. I understand that the background is off screen, but I don’t know what “entering camera mode” means.

You should learn to use your development enviroments debugger. Assuming you’re using flash there’s a good guide here

 
avatar for MoonlaughMaster MoonlaughMaster 6660 posts
Flag Post

You can edit your post. It’s right under your name on the side of the post.

 
avatar for UnknownGuardian UnknownGuardian 8136 posts
Flag Post

I’ve edited his posts and combined them in the first. As MoonlaughMaster says, for the future, the edit button is on the left right under your username and “x posts”.

 
avatar for SSDRWH0 SSDRWH0 27 posts
Flag Post

xDistance and xSpeed are practically the same thing, though maybe I should hv it like xBaseSpeed and xAcceleration (respectively).

when I said “entering camera mode”, I mean like switching from controlling the unit to scrolling the background without controlling the unit.

I’ll hv a go at the try/catch functions (and other methods besides tracing) to hv a look at the problem.

Thanks for replying.

Quick update: The background doesn’t go offscreen at over halfway point of the background after I disable control of my unit, it goes offscreen after the background moved to the right with the controlled unit. I think there might be a proplem coming from the function above the moveCamera function.

 
avatar for SSDRWH0 SSDRWH0 27 posts
Flag Post

More Update: I think there might have to do something with the maths.abs function after I switch from unit mode ot camera mode. The camera will somehow go over 0 regardless of it’s current position. I hope this information would help those helping me…