Skip to main content
anamariaf97539161
Participating Frequently
July 3, 2017
Answered

keyboard function not reaching the array

  • July 3, 2017
  • 1 reply
  • 573 views

Good afternoon,

   I tried to rotate an array of objects with a mouse click, but it did not work for me, so I tried the keyboard instead, but the only way I get it to work is if I use the object name instead of the array. I get the following error message and I do not know how to fix it. Help/suggestions are appreciated.

airplane.rotation = rotationValue

Error #1010: A term is undefined and has no properties.

This is what I have:

stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardDown);

stage.addEventListener(KeyboardEvent.KEY_UP, onKeyboardUp);

addEventListener(Event.ENTER_FRAME, onEnter);


var rotationSpeed:int = 0;

var rotationValue:int = 0;


var airplane:Array = [Memphis_Belle,Pitts,Spruce_Goose,Electra]

for (var i = 0; i < airplane.length; i++)

airplane.addEventListener(MouseEvent.MOUSE_DOWN, beginDrag);


function beginDrag(event:MouseEvent):void {
event.target.startDrag();

}

stage.addEventListener(MouseEvent.MOUSE_UP, finishDrag);
function finishDrag(event:MouseEvent):void {
event.target.stopDrag();

}
function onKeyboardDown(event:KeyboardEvent):void {
if(event.keyCode == Keyboard.LEFT) {
rotationSpeed = -5;
}
if(event.keyCode == Keyboard.RIGHT)

{

rotationSpeed = 5;
}

}

function onKeyboardUp(event:KeyboardEvent):void {

if(event.keyCode == Keyboard.LEFT)

{

rotationSpeed = -0;

}
if(event.keyCode == Keyboard.RIGHT)

{
rotationSpeed = 0;
}

}
function onEnter(event:Event):void

{
rotationValue += rotationSpeed;

airplane.rotation = rotationValue;
}

This topic has been closed for replies.
Correct answer Colin Holgate

Thank you, no more errors, but the objects rotate all at the same time. Is there a focus function, so that only the selected one rotates when the keys are pressed?

I'm sorry, I am new at this.


You only want the one that you're dragging to rotate?

In the beginDrag function you can see what target it was, with event.target.name. You could save that in a variable, and then in the onEnter function you check the name (say the variable is 'currentPlane'):

var currentPlane:String = "";

function beginDrag(event:MouseEvent):void {

       currentPlane = event.target.name;

       event.target.startDrag();

}

function onEnter(event:Event):void     {

       rotationValue += rotationSpeed;

      for(var i = 0; i<airplane.length; i++){

          if(airplane.name==currentPlane){

              airplane.rotation = rotationValue;

              break;

          }

     }

}

The break is there because you've already found the right plane.

There is a problem though in that the next different plane you drag will jump to the rotation the earlier one had reached.

1 reply

Colin Holgate
Inspiring
July 3, 2017

In this part of your code:

for (var i = 0; i < airplane.length; i++)

airplane.addEventListener(MouseEvent.MOUSE_DOWN, beginDrag);

you are missing the braces. It should be:

for (var i = 0; i < airplane.length; i++){

     airplane.addEventListener(MouseEvent.MOUSE_DOWN, beginDrag);

}

In the onEnter function you are missing the for loop, it should be:

rotationValue += rotationSpeed;

for (var i = 0; i < airplane.length; i++){

    airplane.rotation = rotationValue;

}

anamariaf97539161
Participating Frequently
July 3, 2017

Thank you, I fixed the mistakes, but I still get the same error message:

airplane.rotation = rotationValue;

Error #1010: A term is undefined and has no properties.

Colin Holgate
Inspiring
July 3, 2017

Did you add the for loop?:

rotationValue += rotationSpeed;

for (var i = 0; i < airplane.length; i++){

    airplane.rotation = rotationValue;

}

Can you paste all of your script again?