Be a Supporter!

MovieClip Rotation

  • 301 Views
  • 27 Replies
New Topic Respond to this Topic
Jawnduss
Jawnduss
  • Member since: Jul. 18, 2012
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
MovieClip Rotation 2014-02-25 01:40:46 Reply

It seems pretty simple, basically I have the movement code for an asteroids type game, but to make it look more alive i would like to make it so that when the char. moves left, the movieclip rotates up to a maximum of say, 15. This; i was able to make happen. however i tried to make it so that pressing UP would return it to 0, and though i could get it to return to 0, i couldn't get it to return to 0 periodically say, in intervals of 3.

Jawnduss
Jawnduss
  • Member since: Jul. 18, 2012
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to MovieClip Rotation 2014-02-25 01:41:54 Reply

Here is the current code for the movement of my game

import flash.ui.Keyboard;
import flash.events.Event;
import flash.events.KeyboardEvent;

stage.addEventListener(Event.ENTER_FRAME, update);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);

var keys:Array = [];

function update(e:Event):void
{
	if (keys[Keyboard.RIGHT])
	{
		spaceShip.rotation +=  3;
		if (spaceShip.rotation >= 15)
		{
			spaceShip.rotation -=  3;
		}
		spaceShip.x +=  7;
	}

	if (keys[Keyboard.LEFT])
	{
		spaceShip.rotation -=  3;
		if (spaceShip.rotation <= -15)
		{
			spaceShip.rotation +=  3;
		}
		spaceShip.x -=  7;
	}
	if (keys[Keyboard.UP])
	{
		spaceShip.y -=  7;
	}
	if (keys[Keyboard.DOWN])
	{
		spaceShip.y +=  3;
	}
	if (keys[Keyboard.UP,false])
	{
		trace("what");
	}
}

function onKeyDown(e:KeyboardEvent):void
{
	keys[e.keyCode] = true;
}

function onKeyUp(e:KeyboardEvent):void
{
	keys[e.keyCode] = false;
}
milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to MovieClip Rotation 2014-02-25 05:23:11 Reply

At 2/25/14 01:40 AM, Jawnduss wrote: It seems pretty simple, basically I have the movement code for an asteroids type game,

That's not the movement code for an asteroids game.

In asteroids, the movement is directional, meaning that when you press up, the ship goes into the direction it's pointing.

however i tried to make it so that pressing UP would return it to 0, and though i could get it to return to 0,

In your code, the UP key does nothing with the rotation whatsoever.
The rotation is never set to 0 anywhere in your code.

i couldn't get it to return to 0 periodically say, in intervals of 3.

I don't really understand what you mean here, do you want the rotation to slowly go back to 0 while somebody presses UP? Or should this happen regardless if UP is pressed?

At 2/25/14 01:41 AM, Jawnduss wrote: var keys:Array = [];

use an object instead.

if (keys[Keyboard.UP,false])
{
trace("what");
}

That's just wrong, what do you want to do here?

Jawnduss
Jawnduss
  • Member since: Jul. 18, 2012
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to MovieClip Rotation 2014-02-25 12:08:52 Reply

At 2/25/14 05:23 AM, milchreis wrote:

This is what i was attempting to accomplish

I don't really understand what you mean here, do you want the rotation to slowly go back to 0 while somebody presses UP?

But this is ideally what i want.

:Or should this happen regardless if UP is pressed?

And this was a mistake, i was experimenting with something that i forgot to take out before posting this.

if (keys[Keyboard.UP,false])
{
trace("what");
}
That's just wrong, what do you want to do here?
milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to MovieClip Rotation 2014-02-25 12:29:56 Reply

At 2/25/14 12:08 PM, Jawnduss wrote: But this is ideally what i want.
Or should this happen regardless if UP is pressed?

A very simple way to make the ship go back to its neutral position would be to constantly multiply the rotation.
This makes it turn faster, the further it is away from rotation being 0, and slower when closer to it.

//here's some demo:

package 
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class Main extends Sprite 
	{
		private var ship:Shape;
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			ship = new Shape;
			ship.graphics.beginFill(0xff0000);
			ship.graphics.lineTo(10, 30);
			ship.graphics.lineTo(-10, 30);
			ship.graphics.lineTo(0, 0);
			ship.graphics.endFill();
			
			addChild(ship);
			
			ship.x = ship.y = 200;
			
			addEventListener(Event.ENTER_FRAME, loop);
			stage.addEventListener(MouseEvent.CLICK, click);
		}
		
		private function click(e:MouseEvent):void 
		{
			ship.rotation = Math.random() * 180 - 90;
		}
		
		private function loop(e:Event):void 
		{
			ship.rotation *= .9;  // this causes the rotation to go back to 0.
		}
		
	}
	
}

If you want a constant speed of rotation, you have to subtract from it.
This would also require checking when 0 is reached.

Jawnduss
Jawnduss
  • Member since: Jul. 18, 2012
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to MovieClip Rotation 2014-02-25 12:54:38 Reply

I hate to ask, but what are the chances you can add comments to this to help me understand it?
I'm still on a beginner level with AS3, and there's somethings going on this code that i don't understand. Such as: The Shape variable type and the function init.

At 2/25/14 12:29 PM, milchreis wrote: //here's some demo:

package
{
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;

public class Main extends Sprite
{
private var ship:Shape;

public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}

private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point

ship = new Shape;
ship.graphics.beginFill(0xff0000);
ship.graphics.lineTo(10, 30);
ship.graphics.lineTo(-10, 30);
ship.graphics.lineTo(0, 0);
ship.graphics.endFill();

addChild(ship);

ship.x = ship.y = 200;

addEventListener(Event.ENTER_FRAME, loop);
stage.addEventListener(MouseEvent.CLICK, click);
}

private function click(e:MouseEvent):void
{
ship.rotation = Math.random() * 180 - 90;
}

private function loop(e:Event):void
{
ship.rotation *= .9; // this causes the rotation to go back to 0.
}

}

}

If you want a constant speed of rotation, you have to subtract from it.
This would also require checking when 0 is reached.

If it's too much to ask I understand, but I'm eager to learn and I'll do anything to do so.

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to MovieClip Rotation 2014-02-25 14:05:07 Reply

At 2/25/14 12:54 PM, Jawnduss wrote: I hate to ask,

That's a problem. You should feel comfortable asking questions.

but what are the chances you can add comments to this to help me understand it?

I already did.
I commented the line that's of importance for your problem.

The code just does what I explained in my post.
It resets the rotation property to 0 over time.

Try to concentrate on that line.

It's not even a programming thing. Take a calculator, type in any number that you like and multiply it by 0.9 and see what happens if you do it repeatedly.

Such as: The Shape variable type

It's a very basic DisplayObject. In my case I used it to draw a triangle, that represents the ship.
In most cases you do not want to use a MovieClip.
So there are other objects you can display, a Shape object being one of them.

However, rotation is something that all DisplayObjects have, so it doesn't really matter that I use something else than MovieClip (which you are probably using)

and the function init.

It's a function that is called at the beginning.

Do you see that other comment "entry point"? That's where the execution starts, so I do some initial things there.
Drawing the ship for example.

Again, this is not of interest for you as you are probably coding on the time line.

---

You have to find out now if that's the effect you are looking for.

Here are 2 options:
1) copy the line from the ENTER_FRAME and put it into your code modified so it manipulates your ship.

2) use my code entirely, therefore, read this link:
http://code.tutsplus.com/tutorials/how-to-use-a-document-class-in-flash--active-3233
My code is a document class. Create another .fla file as suggested in the article.
This allows you to play around with it and see what's going on.

If you have errors, problems or questions, don't hate to ask here.

Jawnduss
Jawnduss
  • Member since: Jul. 18, 2012
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to MovieClip Rotation 2014-02-25 19:07:04 Reply

Here are 2 options:
1) copy the line from the ENTER_FRAME and put it into your code modified so it manipulates your ship.

this helped, thanks :)

2) use my code entirely, therefore, read this link:
http://code.tutsplus.com/tutorials/how-to-use-a-document-class-in-flash--active-3233
My code is a document class. Create another .fla file as suggested in the article.
This allows you to play around with it and see what's going on.

and that last tut you sent me really helped so i'll make sure to check out this one.

If you have errors, problems or questions, don't hate to ask here.

and i'll be sure to ask, thanks :)

Jawnduss
Jawnduss
  • Member since: Jul. 18, 2012
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to MovieClip Rotation 2014-02-25 21:00:07 Reply

So here is my code after trying to switch everything into a .as file

package 
{
	import flash.ui.Keyboard;
	import flash.events.Event;
	import flash.events.KeyboardEvent;

	public class Main extends MovieClip
	{
		public var keys:Array = [];
	}
	//Movement
	//-------------------------------------------------------------------------------------------------

	stage.addEventListener(Event.ENTER_FRAME, update);
	stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
	stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);

	public function update(e:Event):void
	{
	public var backSpeed:Number = 3;
	public var speed:Number = 8;
		
		if (keys[Keyboard.RIGHT])
		{
			spaceShip.rotation +=  3;
			if (spaceShip.rotation >= 15)
			{
				spaceShip.rotation -=  3;
			}
			spaceShip.x +=  speed;
		}
		if (keys[Keyboard.LEFT])
		{
			spaceShip.rotation -=  3;
			if (spaceShip.rotation <= -15)
			{
				spaceShip.rotation +=  3;
			}
			spaceShip.x -=  speed;
		}
		if (keys[Keyboard.UP])
		{
			spaceShip.rotation *=  .5;
			spaceShip.y -=  speed;
		}
		if (keys[Keyboard.DOWN])
		{
			spaceShip.rotation *=  .9;
			spaceShip.y +=  backSpeed;
		}
	}

	public function onKeyDown(e:KeyboardEvent):void
	{
		keys[e.keyCode] = true;
	}

	public function onKeyUp(e:KeyboardEvent):void
	{
		keys[e.keyCode] = false;
	}

	//Movement
	//-------------------------------------------------------------------------------------------------

	//Scrolling and Boarder Management
	//-------------------------------------------------------------------------------------------------

	stage.addEventListener( Event.ENTER_FRAME, onEnterFrame );
	public function onEnterFrame( evt:Event ):void
	{
		spaceShip.y +=  3;
		Background1.y +=  5;
		if (Background1.y > 700)
		{
			Background1.y = 0;
		}
		Background2.y +=  5;
		if (Background2.y > 700)
		{
			Background2.y = 0;
		}
		if (spaceShip.x > 400)
		{
			spaceShip.x -=  7;
		}
		if (spaceShip.x < 0)
		{
			spaceShip.x +=  7;
		}
		if (spaceShip.y > 615)
		{
			spaceShip.y -=  3;
		}
		if (spaceShip.y < 15)
		{
			spaceShip.y +=  7;
		}

	}
	//Scrolling and Boarder Management
	//-------------------------------------------------------------------------------------------------
}

I'm getting an error 1114: The public attribute can only be used inside a package.
I don't really trust myself on having transferred to code to package correctly, but if someone can show me what i did wrong it would really help me understand how to do it better.

Jawnduss
Jawnduss
  • Member since: Jul. 18, 2012
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to MovieClip Rotation 2014-02-26 00:06:24 Reply

Just doing some research and here's something i found out that i need some help to clarifying.
In the code I recently posted I have some event listeners that happen on stage.
Clearly this is wrong considering the stage would then have to be added to the stage before the event listener can listen for the event.
Now how the hell are you supposed to go about that?
I though this transition from writing on frame to class documents would be easier than this, I mean not much easier but still.
If anyone can clarify the above for me, i would really appreciate it.

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to MovieClip Rotation 2014-02-26 05:21:25 Reply

Take a look at this, "Understanding Classes in AS3":
http://www.untoldentertainment.com/blog/flash-and-actionscript-911/
It's written to help people transition from timeline to class based code.

At 2/25/14 09:00 PM, Jawnduss wrote: So here is my code after trying to switch everything into a .as file

package
{
public class Main extends MovieClip
{
public var keys:Array = [];
}
}

I'm getting an error 1114: The public attribute can only be used inside a package.

First of all, as pointed out earlier, use an Object, not an array.

public var keys:Object = {};

Basically speaking, all the code you write should go into the class block.
In your case, only the code I quoted is within that block.

Additionally, all code that is not a variable definition, should be within a function.
Take a look at my code again.

In your code, these lines are "floating" around in the package block, which breaks the first "rule":

stage.addEventListener(Event.ENTER_FRAME, update);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);

according to the second rule, this should be within a function.

You want to run this code when the application starts.
So it should go into the constructor function.

Due to something explained later, you do not put it there directly.
For now, just copy the constructor from my example and create an init function where you will put the code into that runs once on start up.

ok, so does that solve the error?
No. Errors usually point to a line that cause them.

public function update(e:Event):void
{
public var backSpeed:Number = 3;

this is one candidate
A variable in a function is local to that function, so it makes no sense to add public in front of it.

stage.addEventListener(Event.ENTER_FRAME, update);
stage.addEventListener( Event.ENTER_FRAME, onEnterFrame );

Only add one ENTER_FRAME, otherwise things can get confusing quickly.
Try to merge both functions together into one.
Right now, they just happen one after the other anyway, so there's no reason to overcomplicate things by using 2 event listeners

At 2/26/14 12:06 AM, Jawnduss wrote: Just doing some research

good

In the code I recently posted I have some event listeners that happen on stage.
Clearly this is wrong considering the stage would then have to be added to the stage before the event listener can listen for the event.

A lot of people confuse this.
Here's what's important:

The "stage" in your code is not the timeline in flash.
Hence I use the term stage and timeline to distinguish them.

stage is the top most DisplayObjectContainer that kind of represents the window of the flash player. (that'S why you can get the window dimensions from it, for example)
When your .swf is executed it basically creates an object from your document class (Main.as)which is added to that container.
that's why you can use stage in the constructor.

BUT that's where the init function comes into play.
stage is only available in your document class'es constructor. In any other DisplayObject, you have to wait for it to be available. So if you use your Main.as somewhere else, where it is not the document class, code will fail.
That's why there's nothing in my constructor and all in the init function.

Now how the hell are you supposed to go about that?

Whenever you need the stage, you have to listen for this ADDED_TO_STAGE Event.

I though this transition from writing on frame to class documents would be easier than this, I mean not much easier but still.

Try to go through the link I posted, it should clarify a few things.

HappyWhaleStudios
HappyWhaleStudios
  • Member since: Feb. 1, 2013
  • Online!
Forum Stats
Member
Level 07
Game Developer
Response to MovieClip Rotation 2014-02-26 17:12:25 Reply

At 2/25/14 09:00 PM, Jawnduss wrote: I'm getting an error 1114: The public attribute can only be used inside a package.
I don't really trust myself on having transferred to code to package correctly, but if someone can show me what i did wrong it would really help me understand how to do it better.

All of your code has to go inside the curly brackets for the line

public class Main extends MovieClip

and that will solve that error. Look at some tutorials on how classes and Object Oriented Programming works.


BBS Signature
Jawnduss
Jawnduss
  • Member since: Jul. 18, 2012
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to MovieClip Rotation 2014-02-27 01:03:13 Reply

At 2/26/14 05:21 AM, milchreis wrote: Take a look at this, "Understanding Classes in AS3":
http://www.untoldentertainment.com/blog/flash-and-actionscript-911/
It's written to help people transition from timeline to class based code.

So i tried this one http://www.untoldentertainment.com/blog/2009/09/09/tutorial-understanding-classes-in-as3-part-2/
I attempted to follow along with the example but, It wouldn't work for me.
I created the symbol "A Dog Symbol" Linked it to the class Dog, saved the .as file as Dog.as than literally copied and pasted all the code (including the addChild example) to the .as, saved both and tested, and no trace.

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to MovieClip Rotation 2014-02-27 04:34:13 Reply

At 2/27/14 01:03 AM, Jawnduss wrote: saved the .as file as Dog.as than literally copied and pasted all the code (including the addChild example) to the .as, saved both and tested, and no trace.

Into which file did you copy the addChild code?
Hopefully not into Dog.as

Jawnduss
Jawnduss
  • Member since: Jul. 18, 2012
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to MovieClip Rotation 2014-02-27 12:15:15 Reply

At 2/27/14 04:34 AM, milchreis wrote: Into which file did you copy the addChild code?
Hopefully not into Dog.as

it was Dog.as, it didn't specify where to put it.

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to MovieClip Rotation 2014-02-27 12:21:53 Reply

At 2/27/14 12:15 PM, Jawnduss wrote:
At 2/27/14 04:34 AM, milchreis wrote: Into which file did you copy the addChild code?
Hopefully not into Dog.as
it was Dog.as, it didn't specify where to put it.

Think logically. Why would you want to add the dog to itself?

Go back to the first part and understand that.
There's a MovieClip added directly via its class Ball. As the Tutorial states, you should be familiar with how that works.
The second part creates a dog instead of a ball with the difference that you now create a class file for it.

to sum it up:
part 1: use the class name to add some MovieClip from the library to the timeline dynamically, doing this from the document class instead of the timeline

part 2: don't just use the class name but also define the class to make it actually do something

Jawnduss
Jawnduss
  • Member since: Jul. 18, 2012
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to MovieClip Rotation 2014-02-27 12:37:52 Reply

At 2/27/14 12:21 PM, milchreis wrote: Think logically. Why would you want to add the dog to itself?

So then we would be adding the dog to the stage.

So then I would have to create a separate .as file (Main) and do this:

package 
{
	import flash.display.MovieClip;
	public class Main extends MovieClip
	{
		var dog:MovieClip = new Dog();

		public function Main();
		{
			addChild(dog);
		}
	}
};
Jawnduss
Jawnduss
  • Member since: Jul. 18, 2012
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to MovieClip Rotation 2014-02-27 12:43:32 Reply

Now it's telling me that "dog" is an undefined properties and "addChild" is an undefined method.

but the var dog is given under the class definition, and addChild is in the constructor because I want it to do so on start-up.

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to MovieClip Rotation 2014-02-27 13:01:36 Reply

Your semicolons screw things up.

Semicolons basically show the end of a statement.
You don't want to end your constructor before it even begins, do you?

try this:

package 
{
	import flash.display.MovieClip;
	public class Main extends MovieClip
	{
		private var dog:MovieClip = new Dog(); //make your variables private

		public function Main()  // definitely no semicolon here
		{
			addChild(dog);
		}
	}
} // no semicolon here
Jawnduss
Jawnduss
  • Member since: Jul. 18, 2012
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to MovieClip Rotation 2014-02-27 21:05:45 Reply

This worked fantastically, and i feel like it helps me to understand the context of class files better.
however, I don't understand how this could help me with the situation of my project.

package 
{
	import flash.ui.Keyboard;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.display.MovieClip;

	public class Main extends MovieClip
	{
		var keys:Object = {};

		public function Main():void
		{
			if (stage)
			{
				init();
			}
			else
			{
				addEventListener(Event.ADDED_TO_STAGE, init);
			}
		}
		public function init(e:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
		}


		stage.addEventListener(Event.ENTER_FRAME, update);
		public function update(e:Event):void
		{

			var backSpeed:Number = 3;
			var speed:Number = 8;

			if (keys[Keyboard.RIGHT])
			{
				spaceShip.rotation +=  3;
				if (spaceShip.rotation >= 15)
				{
					spaceShip.rotation -=  3;
				}
				spaceShip.x +=  speed;
			}
			if (keys[Keyboard.LEFT])
			{
				spaceShip.rotation -=  3;
				if (spaceShip.rotation <= -15)
				{
					spaceShip.rotation +=  3;
				}
				spaceShip.x -=  speed;
			}
			if (keys[Keyboard.UP])
			{
				spaceShip.rotation *=  .5;
				spaceShip.y -=  speed;
			}
			if (keys[Keyboard.DOWN])
			{
				spaceShip.rotation *=  .9;
				spaceShip.y +=  backSpeed;
			}
		}

		stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
		public function onKeyDown(e:KeyboardEvent):void
		{
			keys[e.keyCode] = true;
		}

		stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
		public function onKeyUp(e:KeyboardEvent):void
		{
			keys[e.keyCode] = false;
		}
	}
}

It's saying that all of the function names, and the stage are undefined.
Do you know any tutorials for working with the stage?

Jawnduss
Jawnduss
  • Member since: Jul. 18, 2012
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to MovieClip Rotation 2014-02-27 21:19:06 Reply

Okay, so still working with the dog example i tried this based on a combination of stuff from that article, and the code you pasted:

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

	public class Main extends MovieClip
	{
		private var dog:MovieClip = new Dog();
		public function Main()
		{
			addChild(dog);
			if (stage)
			{
				init();
			}
			else
			{
				addEventListener(Event.ADDED_TO_STAGE, init);
			}
			function init(e:Event = null):void
			{
				removeEventListener(Event.ADDED_TO_STAGE, init);
			}
		}
		stage.addEventListener(Event.ENTER_FRAME, moveDog);
		public function moveDog(Event)
		{
			dog.x +=  5;
		}

	}
}

I don't get any errors, but the dog.x += 5; doesn't happen.

Jawnduss
Jawnduss
  • Member since: Jul. 18, 2012
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to MovieClip Rotation 2014-02-27 21:21:48 Reply

At 2/27/14 09:19 PM, Jawnduss wrote: I don't get any errors, but the dog.x += 5; doesn't happen.

Strike that, I get the same error as from my other post, it isn't recognizing the functions or the stage.

Sam
Sam
  • Member since: Oct. 1, 2005
  • Offline.
Forum Stats
Moderator
Level 19
Programmer
Response to MovieClip Rotation 2014-02-27 21:24:35 Reply

Your code needs to be in functions, but your addEventListeners are not.

Jawnduss
Jawnduss
  • Member since: Jul. 18, 2012
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to MovieClip Rotation 2014-02-27 21:29:46 Reply

At 2/27/14 09:24 PM, Sam wrote: Your code needs to be in functions, but your addEventListeners are not.

like this?

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

	public class Main extends MovieClip
	{
		private var dog:MovieClip = new Dog();
		public function Main():void
		{
			addChild(dog);
			if (stage)
			{
				init();
			}
			else
			{
				addEventListener(Event.ADDED_TO_STAGE, init);
			}
			function init(e:Event = null):void
			{
				removeEventListener(Event.ADDED_TO_STAGE, init);
			}
		}
		public function moveDog(Event)
		{
			stage.addEventListener(Event.ENTER_FRAME, moveDog);
			dog.x +=  5;
		}

	}
}

because this doesn't have any errors, but it also doesn't add 5 to x.

Sam
Sam
  • Member since: Oct. 1, 2005
  • Offline.
Forum Stats
Moderator
Level 19
Programmer
Response to MovieClip Rotation 2014-02-27 22:18:12 Reply

How does your computer know to run your moveDog method? You haven't told it to. Sure, you've said to run it every frame with your event listener, but that piece of code is located inside your moveDog method - which never gets run, so therefore the addEventListener never gets run either. Do you see the problem?

Similarly, you have an event listener that will call a method named init, why is this function inside another function?

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

	public class Main extends MovieClip
	{
		private var dog:MovieClip = new Dog();
		public function Main():void
		{
                        // If the stage is ready
			if (stage)
			{
                                // Call init
				init();
			}
			else
			{ 
                                // If it's not, we'll add an event listener which calls init when the stage is ready
				addEventListener(Event.ADDED_TO_STAGE, init);
			}
		}
               
                private function init(e:Event = null):void
                {
                    // This is the entry point to your game, all your coding starts here!

                    // Let's add the dog to this class so we can see him
                    addChild(dog);

                    // Listen for a frame update, and whenever that happens, call the moveDog method
                    addEventListener(Event.ENTER_FRAME, moveDog);
                }
                
                // Notice when a method is triggered by an event listener, it takes a parameter of event that I've called e (but you can call it evnt or evt or whatever you want
		public function moveDog(e:Event)
		{
                        // What does having that parameter variable do? Let's look at this:
                        trace(e.currentTarget)
                        // The above trace will output the currentTarget of the event listener that triggered this event method call
                        // It's a bit of a mouthful but basically because you added the event listener to "this" (Main, this class), the current target is this.

                        // Manipulate our dogs x property
			dog.x +=  5;
		}

	}
}

While your enthusiasm is great, you're making very basic mistakes. Your init was a separate function a few examples ago, and along the way it somehow made its way into your Main function (the constructor).

Here's a bit of advice: forget about the stage for now. Your Main class is your entry point to your game - you don't need the stage for things like addChild, addEventListener.

Jawnduss
Jawnduss
  • Member since: Jul. 18, 2012
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to MovieClip Rotation 2014-02-27 22:56:23 Reply

At 2/27/14 10:18 PM, Sam wrote: While your enthusiasm is great, you're making very basic mistakes. Your init was a separate function a few examples ago, and along the way it somehow made its way into your Main function (the constructor).

Here's a bit of advice: forget about the stage for now. Your Main class is your entry point to your game - you don't need the stage for things like addChild, addEventListener.

Do you think you could give me somewhat of a practice problem?
something that'll help grasp the idea a little better.
I'm trying to learn how to do this because it's organized better and make's things more simple down the road, but right now i'm struggling to understand how it works and implement it into my current project.

Sam
Sam
  • Member since: Oct. 1, 2005
  • Offline.
Forum Stats
Moderator
Level 19
Programmer
Response to MovieClip Rotation 2014-02-27 23:30:21 Reply

At 2/27/14 10:56 PM, Jawnduss wrote: Do you think you could give me somewhat of a practice problem?
something that'll help grasp the idea a little better.
I'm trying to learn how to do this because it's organized better and make's things more simple down the road, but right now i'm struggling to understand how it works and implement it into my current project.

The setup you have now is pretty straight forward and demonstrates classes, albeit a little simple. This seems to be a good starting point (haven't had time to read through it all, just skimmed), but I'd still recommend the read.

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to MovieClip Rotation 2014-02-28 06:17:51 Reply

At 2/27/14 11:30 PM, Sam wrote: This seems to be a good starting point

I second that. Dru's 101 is as good as it gets when it comes to online articles for basic As3.
It is part of a whole series: http://code.tutsplus.com/series/as3-101--active-7395
I suggested Ryan's over it because it's dedicated to people going from timeline code to document classes.

At 2/27/14 09:05 PM, Jawnduss wrote: I don't understand how this could help me with the situation of my project.

That's the thing. Your questions related to tutorials are a little off topic.
Here's a suggestion: If you follow along both or either of the two mentioned tutorials and you get into trouble, ask for help in a different thread.

This thread is about the mc rotation.
You never clearly said if the effect is to your liking or not. If it is, I guess this means problem solved and let's leave it at that.