Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

keyboard function not reaching the array

Community Beginner ,
Jul 02, 2017 Jul 02, 2017

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

TOPICS
ActionScript
467
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jul 03, 2017 Jul 03, 2017

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 =

...
Translate
LEGEND ,
Jul 02, 2017 Jul 02, 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;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 02, 2017 Jul 02, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 02, 2017 Jul 02, 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 02, 2017 Jul 02, 2017

yes, I did add the for loop. Here it is with the corrections:

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;
      for(var i = 0; i<airplane.length; i++){
      airplane.rotation = rotationValue;

     }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 03, 2017 Jul 03, 2017

This is missing a close brace:

function onEnter(event:Event):void

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

     }

should be:

function onEnter(event:Event):void

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

     }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 03, 2017 Jul 03, 2017

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 03, 2017 Jul 03, 2017

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 03, 2017 Jul 03, 2017
LATEST

You are right, it does jump to the previous object position. My application also crash when I attempt to rotate the second object

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines