Skip to main content
Participant
December 15, 2008
Question

adEventListener

  • December 15, 2008
  • 1 reply
  • 298 views
I am trying to learn something about the codes in actionscript 3. And a friend of my had a book about it so I start trying some actions and somewhere in the book stood the following thing:

var vierkant:Object = new Object()
var box:Object = new Object()
vierkant.addEventListener(MouseEvent.MOUSE_UP,
onRotateRight);
function onRotateRight(evt:MouseEvent):void {
box.rotation += 20;
}

So I made an object and gave it the name vierkant and another object with the name box
but when I write this in the action window and then click (enter+ctrl)
it gives a error in output, but I have no idea what I'm doing wrong.
This topic has been closed for replies.

1 reply

Inspiring
December 15, 2008

I would have expected another error, but in essence you are trying to
let something do something it can't.

Generic objects don't recognize the addEventListener method.

Something like this would give no errors (allthough be pretty useless):

var vierkant:Sprite = new Sprite();
var box:Sprite = new Sprite ();
vierkant.addEventListener(MouseEvent.MOUSE_UP, onRotateRight);
function onRotateRight(evt:MouseEvent):void {
box.rotation += 20;
}

Here 'vierkant' and 'box' are said/set to be Sprites which does
recognize the addEventListener method. It's useless because the Sprites
are never shown, even if they had substance (which they don't).

To give them substance, use:

var vierkant:Sprite = new Sprite();
with( vierkant.graphics )
{
beginFill( 0xFF0000 );
drawRect( 0, 0, 50, 50 );
endFill();
}
addChild( vierkant);

var box:Sprite = new Sprite();
with( box.graphics )
{
beginFill( 0xFF0000 );
drawRect( 0, 0, 50, 50 );
endFill();
}
box.x = 100;
box.y = 100;
addChild( box );

vierkant.addEventListener(MouseEvent.MOUSE_UP, onRotateRight);
function onRotateRight(evt:MouseEvent):void {
box.rotation += 20;
}

Here in both 'vierkant' and 'box' rectangles are created so they have
substance. They are added to the displayList (a construct of 'things'
potentially being shown on stage) so you can see them and interact with
them.

HTH (somewhat)
Manno

--
----------
Manno Bult
http://www.aloft.nl