Jump to content
  • Advertisement
Sign in to follow this  
dleanjeanz

Almost the same code but different action

This topic is 1441 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm coding a paddle in a game following this tutorial: http://www.flashgametuts.com/tutorials/as3/how-to-create-a-brick-breaker-game-in-as3-part-1/

 

And this is my Main.as file:

package 
{	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;

	 [SWF(backgroundColor = "0x000000")]
	public class Main extends Sprite 
	{
		public var mcPaddle:MovieClip = new MovieClip();
		
		
		public function beginCode():void {
			mcPaddle.graphics.beginFill(0xffffff);
			mcPaddle.graphics.drawRect(0, 0, 55, 10);
			mcPaddle.graphics.endFill();
			mcPaddle.addEventListener(Event.ENTER_FRAME, movePaddle);
		}
		
		public function movePaddle(event:Event):void {
			var tempX:Number = new Number();
			tempX = mouseX - mcPaddle.width / 2;
			
			if (mouseX < mcPaddle.width / 2) {
				tempX = 0;
			}
			
			if (mouseX > stage.stageWidth - mcPaddle.width / 2) {
				tempX = stage.stageWidth - mcPaddle.width;
			}
			
			mcPaddle.x = tempX;
			mcPaddle.y = stage.stageHeight - mcPaddle.height * 3;
		}
		
		public function Main():void 
		{
			beginCode();
		}
		
	}
	
}

It works very well just like the preview at the end of the link above.

 

Then I wrote it in the OOP way:

Main.as:

package 
{	
	import flash.display.Sprite;
	import gameobjects.paddle.Paddle;

	 [SWF(backgroundColor = "0x000000")]
	public class Main extends Sprite 
	{

		public function Main():void 
		{
			var mcPaddle:Paddle = new Paddle();
			addChild(mcPaddle);
		}
		
	}
	
}

Paddle.as:

package gameobjects.paddle 
{
	import flash.display.MovieClip;
	import flash.events.Event;

	public class Paddle extends MovieClip
	{
		
		public function Paddle() 
		{
			this.graphics.beginFill(0xffffff);
			this.graphics.drawRect(0, 0, 55, 10);
			this.graphics.endFill();
			this.addEventListener(Event.ENTER_FRAME, movePaddle);
		}
		
		public function movePaddle(event:Event):void {
			var tempX:Number = new Number();
			tempX = mouseX - this.width / 2;
			
			if (mouseX < this.width / 2) {
				tempX = 0;
			}
			
			if (mouseX > stage.stageWidth - this.width / 2) {
				tempX = stage.stageWidth - this.width;
			}
			
			this.x = tempX;
			this.y = stage.stageHeight - this.height * 3;
		}
	}

}

You see they're almost the same code

but this one doesn't work so well: http://goo.gl/p0FHBj

 

Any ideas? Thanks

Share this post


Link to post
Share on other sites
Advertisement

Nevermind, the compiler is just confused, I added "stage." before "mouseX" in "Paddle.as" and it works well:

package gameobjects.paddle 
{
	import flash.display.MovieClip;
	import flash.events.Event;

	public class Paddle extends MovieClip
	{
		
		public function Paddle() 
		{
			this.graphics.beginFill(0xffffff);
			this.graphics.drawRect(0, 0, 55, 10);
			this.graphics.endFill();
			this.addEventListener(Event.ENTER_FRAME, movePaddle);
		}
		
		public function movePaddle(event:Event):void {
			var tempX:Number = new Number();
			tempX = stage.mouseX - this.width / 2;
			
			if (stage.mouseX < this.width / 2) {
				tempX = 0;
			}
			
			if (stage.mouseX > stage.stageWidth - this.width / 2) {
				tempX = stage.stageWidth - this.width;
			}
			
			this.x = tempX;
			this.y = stage.stageHeight - this.height * 3;
		}
	}

}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!