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

Creating Multiple 'Move with keyboard arrows' script on the same frame

New Here ,
Oct 04, 2018 Oct 04, 2018

Hi stressed out uni student here!

I am able to successfully insert the 'move with keyboard arrows' pre-written code for the symbol I made and it works when I test it out. I wanted to add another identical symbol on the same frame but when I do this with a different name and then add the same code it just breaks so now even the first symbol doesn't move with keyboard arrows. Do I need to add something between the 2 codes to separate them? I even tried doing the second symbol on a new layer and it still wouldn't work. I can't do it on another frame and it just flashes then. I'm completely baffled. I want to build up multiple assets on the same frame but if I can't code all of them then it's useless.

TOPICS
ActionScript
424
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 , Oct 05, 2018 Oct 05, 2018

Drag and drop is more likely to work for the coding scenario you are describing since it involves clicking on a specific object.  In the case of keyboard commands that changes... the code needs to know which object you intend to be moving.  The code you show always assumes symbol1 is the object being moved.  If the code you created for symbol2 looked exactly the same, then you are going to have a problem, especially if it is recreating functions with the same name... that will raise errors.

What

...
Translate
LEGEND ,
Oct 04, 2018 Oct 04, 2018

Show your code for both.  Also, explain how you plan to control which one is being moved (do you click on it first to select it) or are they all going to be moved at once?

If the code is using the same function names for different objects, that is likely the beginning of the problem.

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
New Here ,
Oct 04, 2018 Oct 04, 2018

Hi!!

So this is the code that gets put in for the first object:

/* Move with Keyboard Arrows

Allows the specified symbol instance to be moved with the keyboard arrows.

Instructions:

1. To increase or decrease the amount of movement, replace the number 5 below with the number of pixels you want the symbol instance to move with each key press.

Note the number 5 appears four times in the code below.

*/

var upPressed:Boolean = false;

var downPressed:Boolean = false;

var leftPressed:Boolean = false;

var rightPressed:Boolean = false;

symbol1.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);

stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);

stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);

function fl_MoveInDirectionOfKey(event:Event)

{

if (upPressed)

{

symbol1.y -= 5;

}

if (downPressed)

{

symbol1.y += 5;

}

if (leftPressed)

{

symbol1.x -= 5;

}

if (rightPressed)

{

symbol1.x += 5;

}

}

function fl_SetKeyPressed(event:KeyboardEvent):void

{

switch (event.keyCode)

{

case Keyboard.UP:

{

upPressed = true;

break;

}

case Keyboard.DOWN:

{

downPressed = true;

break;

}

case Keyboard.LEFT:

{

leftPressed = true;

break;

}

case Keyboard.RIGHT:

{

rightPressed = true;

break;

}

}

}

function fl_UnsetKeyPressed(event:KeyboardEvent):void

{

switch (event.keyCode)

{

case Keyboard.UP:

{

upPressed = false;

break;

}

case Keyboard.DOWN:

{

downPressed = false;

break;

}

case Keyboard.LEFT:

{

leftPressed = false;

break;

}

case Keyboard.RIGHT:

{

rightPressed = false;

break;

}

}

}

I then clicked on the next object and repeated the code but the name would change to symbol2 instead of symbol1 so although they are on the same frame the coding had different names. Animate did that automatically for me. You would click on the object first and then move it as that's what you have to do anyway if there is only one object but I don't know if you'd need coding for more than 1. I did the exact same for 'drag and drop' and it worked perfectly.

Thanks for taking the time to help!!

Gina

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 ,
Oct 05, 2018 Oct 05, 2018

Drag and drop is more likely to work for the coding scenario you are describing since it involves clicking on a specific object.  In the case of keyboard commands that changes... the code needs to know which object you intend to be moving.  The code you show always assumes symbol1 is the object being moved.  If the code you created for symbol2 looked exactly the same, then you are going to have a problem, especially if it is recreating functions with the same name... that will raise errors.

What you oughta do is change that code to be generic such that it targets whatever object you intend to be moved.  That can be as simple as changing "symbol1" to something generic, like "selectedObject" in those functions.

var selectedObject:MovieClip;  // you will assign the selected object to this when you select it - for now it is null

symbol1.addEventListener(MouseEvent.CLICK, assignAsSelectedObject);

symbol2.addEventListener(MouseEvent.CLICK, assignAsSelectedObject);

symbol3.addEventListener(MouseEvent.CLICK, assignAsSelectedObject);

function assignAsSelectedObject(evt:MouseEvent){

     // you probably want to remove the event listener before you change the selectedObject, but only if the selectedObject is not null

     if(selectedObject){

                  selectedObject.removeEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);

     }

     selectedObject = MovieClip(evt.currentTarget); // this assigns the selectedObject to whichever object was the coded target of the event

     selectedObject.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);

}

And in the function you show, change "symbol1" to "selectedObject" so that it points to whichever object was clicked on.  All of the objects will share this same function

The code I provide above is pretty much derived from memory, so don't get too disappointed if copying/pasting it doesn't work.  You might have to fiddle with it, but the concept is there.

Someone else might have another approach to consider as well.

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 ,
Oct 05, 2018 Oct 05, 2018
LATEST

So anyways, I was able to create a quick working version using my suggestion and the code needed for having keyboard-movable symbols goes like what follows.  You just need to add the click event listeners for each symbol you want to be able to move... just be sure to assign them the instance names you intend to use (in this example they are named as you started to do.... symbol1, etc).

var selectedObject:MovieClip;  // you will assign the selected object to this when you select it - for now it is null

var upPressed:Boolean = false;

var downPressed:Boolean = false;

var leftPressed:Boolean = false;

var rightPressed:Boolean = false;

symbol1.addEventListener(MouseEvent.CLICK, assignAsSelectedObject);

symbol2.addEventListener(MouseEvent.CLICK, assignAsSelectedObject);

symbol3.addEventListener(MouseEvent.CLICK, assignAsSelectedObject);

// etc.... add another for each new symbol you want to move

stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);

stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);

function assignAsSelectedObject(evt:MouseEvent){

     // you probably want to remove the event listener before you change the selectedObject, but only if the selectedObject is not null

     if(selectedObject){

                  selectedObject.removeEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);

     }

     selectedObject = MovieClip(evt.currentTarget); // this assigns the selectedObject to whichever object was the coded target of the event

     selectedObject.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);

}

function fl_UnsetKeyPressed(event:KeyboardEvent):void

{

switch (event.keyCode)

{

case Keyboard.UP:

{

upPressed = false;

break;

}

case Keyboard.DOWN:

{

downPressed = false;

break;

}

case Keyboard.LEFT:

{

leftPressed = false;

break;

}

case Keyboard.RIGHT:

{

rightPressed = false;

break;

}

}

}

function fl_SetKeyPressed(event:KeyboardEvent):void

{

switch (event.keyCode)

{

case Keyboard.UP:

{

upPressed = true;

break;

}

case Keyboard.DOWN:

{

downPressed = true;

break;

}

case Keyboard.LEFT:

{

leftPressed = true;

break;

}

case Keyboard.RIGHT:

{

rightPressed = true;

break;

}

}

}

function fl_MoveInDirectionOfKey(event:Event)

{

if (upPressed)

{

selectedObject.y -= 5;

}

if (downPressed)

{

selectedObject.y += 5;

}

if (leftPressed)

{

selectedObject.x -= 5;

}

if (rightPressed)

{

selectedObject.x += 5;

}

}

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