Copy link to clipboard
Copied
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;
}
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 =
...Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
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;
}
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
You are right, it does jump to the previous object position. My application also crash when I attempt to rotate the second object
Find more inspiration, events, and resources on the new Adobe Community
Explore Now