00:00
00:00
Newgrounds Background Image Theme

BoneMarr0w just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

need an object on stage to rotate

592 Views | 14 Replies
New Topic Respond to this Topic

need an object on stage to rotate 2013-03-14 11:47:02


hey. I'm trying to make a platformer in which the entire world is supposed to move around the player as well as the player being in control of a character.
right now I'm battling with the rotation function in actionscript.

here's my code:
function rotateStage(e:Event){

if(leftKeyPush && rightKeyPush) return;

if(leftKeyPush) World.rotation = 90;
if(rightKeyPush) World.rotation = -90;

}

'leftKeyPush' and 'rightKeyPush' are what detect if the arrow keys are being pressed

My intended function is that when you hit left, the world turns 90 degrees to the left, and vice versa. but it's not doing it, and I'm not sure why
there are no compiler errors (yay. -.-) and the keys have been assigned correctly

thanks for reading

Response to need an object on stage to rotate 2013-03-14 12:03:33


At 3/14/13 11:47 AM, voter96 wrote: My intended function is that when you hit left, the world turns 90 degrees to the left, and vice versa. but it's not doing it, and I'm not sure why
there are no compiler errors (yay. -.-) and the keys have been assigned correctly

thanks for reading

Have you tried putting trace statements in the conditionals to make sure they work? What exactly is World, like what class is it and all?

Response to need an object on stage to rotate 2013-03-14 12:24:10


At 3/14/13 12:03 PM, MSGhero wrote: Have you tried putting trace statements in the conditionals to make sure they work? What exactly is World, like what class is it and all?

World is the instance name of a movieclip which sits on my stage. It's been exported for actionscript.

I'm looking up what trace statements and conditionals are right now, but I'm not entirely sure what you mean by those. the fact I'm having to look them up probably means that I haven't

Response to need an object on stage to rotate 2013-03-14 12:33:59


At 3/14/13 12:24 PM, voter96 wrote:
At 3/14/13 12:03 PM, MSGhero wrote: Have you tried putting trace statements in the conditionals to make sure they work? What exactly is World, like what class is it and all?
World is the instance name of a movieclip which sits on my stage. It's been exported for actionscript.

I'm looking up what trace statements and conditionals are right now, but I'm not entirely sure what you mean by those. the fact I'm having to look them up probably means that I haven't
if (leftKeyPush) { // conditional
   trace("K"); // trace: if "K" gets output, the line below will run; if not, the line below will not run
   World.rotation = 90;
}

Response to need an object on stage to rotate 2013-03-14 12:52:07


At 3/14/13 12:33 PM, MSGhero wrote: if (leftKeyPush) { // conditional
trace("K"); // trace: if "K" gets output, the line below will run; if not, the line below will not run
World.rotation = 90;
}

thanks very much, and it turns out that the line isn't being run at all. my only guess right now is that there's something wrong with the key recognition

Response to need an object on stage to rotate 2013-03-14 14:21:59


if anyone is still reading this, I could still do with some help.

I know that it's not finding any errors in my code, and that it's not detecting when I press keys or release them. I can't see anything wrong with my code

http://pastebin.com/0SE28PDq

here's the code I have for detecting key presses:

function pressKey(k:KeyboardEvent){
switch (k.keyCode){
case(37) : leftKeyPush = true; break;
case(39) : rightKeyPush = true;
}
}//end of pressKey

function releaseKey(k:KeyboardEvent){
switch(k.keyCode){
case(37) : leftKeyPush = false; break;
case(39) : rightKeyPush = false;
}
}//end of releaseKey

Response to need an object on stage to rotate 2013-03-14 15:09:50


package  {
	import flash.events.*;
	import flash.display.MovieClip;
	import flash.media.*;
	import fl.motion.easing.Back;
		
		
	public class Rotating extends MovieClip{
		

		//class variables
		var leftKeyPush, rightKeyPush: Boolean = false;
		
		
		
		public function Rotating() {
			// constructor code
			
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, pressKey);
			stage.addEventListener(KeyboardEvent.KEY_UP, releaseKey);
		}
			
		public function pressKey(k:KeyboardEvent){
			switch (k.keyCode){
				case(37) : leftKeyPush = true; break;
				case(39) : rightKeyPush = true;
			}
		}//end of pressKey
			
		public function releaseKey(k:KeyboardEvent){
			switch(k.keyCode){
				case(37) : leftKeyPush = false; break;
				case(39) : rightKeyPush = false;
			}
		}//end of releaseKey
			
		public function rotateStage(e:Event){
			trace("k");
			if(leftKeyPush) World.rotation = 90;
			if(rightKeyPush) World.rotation = -90;
				
		}

	}
	
}

changed rotate to Rotating, which is actually the constructor of the class, I removed the listeners from the constructor and made them a class method.

Response to need an object on stage to rotate 2013-03-14 16:06:14


At 3/14/13 03:09 PM, GeoKureli wrote: changed rotate to Rotating, which is actually the constructor of the class, I removed the listeners from the constructor and made them a class method.

tried this, same result. no errors, and nothing happened :c

Response to need an object on stage to rotate 2013-03-14 16:14:06


At 3/14/13 04:06 PM, voter96 wrote:
At 3/14/13 03:09 PM, GeoKureli wrote: changed rotate to Rotating, which is actually the constructor of the class, I removed the listeners from the constructor and made them a class method.
tried this, same result. no errors, and nothing happened :c

Probably because you are adding the listeners in the constructor, before it gets added to the stage. Give it an event listener for ADDED_TO_STAGE, and add the keyboard listeners there. I seem to recall that stage is only accessible to a mc on the display list.

Response to need an object on stage to rotate 2013-03-14 16:27:22


At 3/14/13 04:14 PM, Mattster wrote: Probably because you are adding the listeners in the constructor, before it gets added to the stage. Give it an event listener for ADDED_TO_STAGE, and add the keyboard listeners there. I seem to recall that stage is only accessible to a mc on the display list.

sorry, I'm not sure what you mean. what kind of listener would that be?

If I'm reading what you just said right, I should point out that nothing is being put on the stage by coding - World is there already.

Response to need an object on stage to rotate 2013-03-14 16:30:19


At 3/14/13 04:14 PM, Mattster wrote:
At 3/14/13 04:06 PM, voter96 wrote:
At 3/14/13 03:09 PM, GeoKureli wrote: changed rotate to Rotating, which is actually the constructor of the class, I removed the listeners from the constructor and made them a class method.
tried this, same result. no errors, and nothing happened :c
Probably because you are adding the listeners in the constructor, before it gets added to the stage. Give it an event listener for ADDED_TO_STAGE, and add the keyboard listeners there. I seem to recall that stage is only accessible to a mc on the display list.

blah how did I miss this
addEventListener(Event.ADDED_TO_STAGE, init);
then you can reference the stage in init.

also if you got no errors then you're never instantiated your Rotating class (new Rotating())

Response to need an object on stage to rotate 2013-03-14 16:42:41


At 3/14/13 04:30 PM, GeoKureli wrote: blah how did I miss this
addEventListener(Event.ADDED_TO_STAGE, init);
then you can reference the stage in init.

also if you got no errors then you're never instantiated your Rotating class (new Rotating())

like this:
stage.addEventListener(Event.ADDED_TO_STAGE, rotateStage);

or like this?:
stage.addEventListener(Event.ADDED_TO_STAGE, pressKey, releaseKey);

Response to need an object on stage to rotate 2013-03-14 18:18:36


At 3/14/13 04:42 PM, voter96 wrote:
At 3/14/13 04:30 PM, GeoKureli wrote:
like this:
stage.addEventListener(Event.ADDED_TO_STAGE, rotateStage);

only if you want roatateStage to be called once. it seems like you want it to be called every frame

or like this?:
stage.addEventListener(Event.ADDED_TO_STAGE, pressKey, releaseKey);

those functions are key listeners, and you can't pass 2 at once

You're confusing events, here's a reference guide to events
Event.ADDED_TO_STAGE: gets called when the object gets added to the display list so this would be called if you said
addChild(myObj); if myObj has an ADDED_TO_STAGE event, this would trigger it.
Event.ENTER_FRAME: gets called every frame, commonly used for an update function
KeyboardEvent.KEY_DOWN: usually applied like so: stage.addEventListener(KeyboardEvent.KEY_DOWN, myFunc), so that myFunc() gets called whenever a key is pressed
KeybooardEvent.KEY_UP: same as key down, but when a key is released.

these are your tools, use them wisely, and seriously try googling "as3 events for beginners"

Response to need an object on stage to rotate 2013-03-14 18:42:58


At 3/14/13 06:18 PM, GeoKureli wrote:
You're confusing events, here's a reference guide to events
Event.ADDED_TO_STAGE: gets called when the object gets added to the display list so this would be called if you said
addChild(myObj); if myObj has an ADDED_TO_STAGE event, this would trigger it.
Event.ENTER_FRAME: gets called every frame, commonly used for an update function
KeyboardEvent.KEY_DOWN: usually applied like so: stage.addEventListener(KeyboardEvent.KEY_DOWN, myFunc), so that myFunc() gets called whenever a key is pressed
KeybooardEvent.KEY_UP: same as key down, but when a key is released.

these are your tools, use them wisely, and seriously try googling "as3 events for beginners"

okay, I'll google some tut's.
my tutor has not taught me well enough. either that or I just jumped the shark. I probably jumped the shark. I do that a lot.

I do have a question though, I said that nothing is being added to the stage using code, and yet you told me to put in a listener for when something is added to the stage? why?

Response to need an object on stage to rotate 2013-03-14 19:08:31


At 3/14/13 06:42 PM, voter96 wrote: I do have a question though, I said that nothing is being added to the stage using code, and yet you told me to put in a listener for when something is added to the stage? why?

if your just drag things onto the stage in flash, they are still being added to the stage, and you can't call stageAddEventListener until the ADDED_TO_STAGE event is dispatched, which will get called whether you add them in code or not.