Advertisement

Almost the same code but different action

Started by October 11, 2014 06:33 AM
0 comments, last by dleanjeanz 10 years, 3 months ago

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

Twitter: [twitter]DleanJeans[/twitter]

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;
		}
	}

}

Twitter: [twitter]DleanJeans[/twitter]

This topic is closed to new replies.

Advertisement