moving a movie clip by pressing a button AS3 - actionscript-3

Does anyone know how to move a movie clip by clicking a button on the stage. I can get it to move in increments, but I want it to move constantly. Currently I have this:
down.addEventListener(MouseEvent.MOUSE_DOWN, arrowDown);
function arrowDown(event:MouseEvent):void
{
bottomArrow.y += 1;
}

First, you should listen for KeyboardEvents instead of MouseEvent. Then I think you should listen for those events being dispatched by the stage.
Here is an example using the Event.ENTER_FRAME event. If you'd like to control better the speed of you sprite moves you might want to user a timer instead.
This example works when the down arrow is pressed but you can change Keyboard.DOWN with any key you want.
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
function onKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.DOWN)
{
stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
}
function onKeyUp(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.DOWN)
{
stage.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
}
}
function onEnterFrame(event:Event):void
{
bottomArrow.y += 1;
}

Related

Two event listeners doing the same thing (ActionScript 3)

Both of my event listeners are going to the same frame, all answers are appreciated.
stop();
addEventListener(MouseEvent.CLICK, enterGameClick);
function enterGameClick (event:MouseEvent) {
if (enterGame.alpha == 1){
gotoAndPlay(4);
removeEventListener(MouseEvent.CLICK, enterGameClick);
}
else if (creditsButton.alpha == 1) {
gotoAndPlay(3);
removeEventListener(MouseEvent.CLICK, enterGameClick);
}
}
Also, is there any way of selecting an object without having to use identification like alpha?
You can get the event.target or event.currentTarget to identify what you should do.

trigger sound effects when mc hits a mc! FLASH AS3

i have two mcs, i want this mc to hit another mc which then plays a loud bang. How would i do this? I'll be using a keyboard event to go with it as well.
Thanks
Create a new Flash ActionScript 3 document. be save.
Put your sound like names boom.mp3 in same directory.
Draw a square shape on the stage and convert it into a Movie Clip symbol.
Put a total of 2 instances of this Movie Clip symbol on the stage.
Bring up the Actions panel and add the following code.
Code below, simply drag the mouse to mc1 and mc2 collision when sound is played. is a basic collision with sound playback.
import flash.media.Sound;
import flash.events.MouseEvent;
import flash.net.URLRequest;
mc1.addEventListener(MouseEvent.MOUSE_DOWN, drag);
stage.addEventListener(MouseEvent.MOUSE_UP, drop);
var sound:Sound=new Sound();
sound.load(new URLRequest("boom.mp3"));
function drag(e:MouseEvent):void
{
mc1.startDrag();
}
function drop(e:MouseEvent):void
{
mc1.stopDrag();
if (mc1.hitTestObject(mc2))
{
trace("Collision detected!");
sound.play(0);
}
else
{
trace("No collision.");
}
}
If you want using a KeyboardEvent try this:
function playIsCollision(char:DisplayObject, target:DisplayObject):void
{
if(char.hitTestObject(target))
{
sound.play(0);
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
function keyDownHandler(e:KeyboardEvent):void
{
if(e.keyCode == Keyboard.RIGHT)
{
trace("right move");
mc1.x += 1;
playIsCollision(mc1,mc2);
}
else if(e.keyCode == Keyboard.LEFT)
{
trace("left move");
mc1.x -= 1;
playIsCollision(mc1,mc2);
}
else if(e.keyCode == Keyboard.UP)
{
trace("up move");
mc1.y -= 1;
playIsCollision(mc1,mc2);
}
else if(e.keyCode == Keyboard.DOWN)
{
trace("down move");
mc1.y += 1;
playIsCollision(mc1,mc2);
}
}
here is my code: Collision

can you make Keyboard events differ on different pages?

Hey is there a way to have certain keys do one thing on one page and then different on another, as i use
But then on next frame i cant use space bar again to do another task?
Any help would be appreciated
stop();
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDownn);
function myKeyDownn(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.SPACE){
gotoAndPlay("welcome");
}
}
just remove the listener before you leave the frame.
stop();
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDownn);
function myKeyDownn(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.SPACE){
stage.removeEventListener(KeyboardEvent.KEY_DOWN, myKeyDownn);
gotoAndPlay("welcome");
}
}
in the frame "welcome" add the same code again and just change the gotoAndPlay() part.
stop();
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDownn);
function myKeyDownn(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.SPACE){
stage.removeEventListener(KeyboardEvent.KEY_DOWN, myKeyDownn);
gotoAndPlay("a different frame");
}
}

Detect first key-down event only in AS3

In the KeyboardEvent object I get on key-down events, is there a way to know if the event is the first key-down, or a repeat when the key is held down?
I want to toggle a setting when a key is pressed; I could listen to key-up but I'd prefer the action is taken as soon as the key is pushed... as long as I can stop it toggling back and forth as the key-down repeat events are generated.
Tested and works
import flash.events.KeyboardEvent;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onUp);
var keys:Object = {};
function onDown(e:KeyboardEvent):void {
if( ! Boolean(e.keyCode in keys)) {
trace("First Time");
keys[e.keyCode] = true;
}
}
function onUp(e:KeyboardEvent):void {
delete keys[e.keyCode];
}
The idea of storing key codes in an object and the use of delete on key up originally came from Senoculars library. See http://www.senocular.com/flash/actionscript/
The easiest thing is to remove or disable the KEY_DOWN listener with a flag inside the KEY_DOWN listener, and then add or enable it in the KEY_UP.
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
private function onKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.SPACE)
{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
}
private function onKeyUp(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.SPACE)
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.removeEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
}

In Flash Actionscript 3.0 how do I create smooth keyboard controls for player movement?

I am a beginner in Flash Actionscript 3.0 programming. I am trying to create smooth keyboard controls for player movement in a game.
I'm currently using addEventListener(KeyboardEvent.KEY_DOWN) listening for a keyboard key press and then within the handler function moving a graphic by adding a number to its .x or .y property.
This creates a slow, sluggish jerk at the beginning. I know there's a smoother, more responsive way to do this but have no idea where to begin. Any help would be appreciated!
For smooth keys I would suggest using either a Timer or onEnterFrame to poll for keys often enough to get smooth controls. It will get the job done, but at a certain expense. If you've got the rest of the logic all fine, this should fit in ok:
var key:int = NaN;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress,false,0,true);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease,false,0,true);
this.addEventListener(Event.ENTER_FRAME,update,false,0,true);
function onKeyPress(event:KeyboardEvent):void {
key = event.keyCode;
event.stopPropagation();
}
function onKeyRelease(event:KeyboardEvent):void {
key = NaN;
event.stopPropagation();
}
function update(event:Event):void{
if(key) trace(key);
}
I make sure the event doesn't bubble by stopping it's propagation, and it's set on the stage which should be the topmost level, event wise. Also I'm using the key only the key is down, otherwise I ignore it in the enterFrame handler.
HTH,
George
Where are you placing the listener? Is it in the application, or in the sprite that is supposed to move? Does the sprite have the focus when you are pressing the key?
Also, in adding the event listener, are you using capture? That is, are you setting the 3rd argument to true, as in
addEventListener(KeyboardEvent.KEY_DOWN, yourHandler, true)
If you use capture, which is how you have to do it if the App itself is listening for the event, then you will get a certain amount of latency, and this latency will be greater the more complex the interface is. If those events have to work their way up a vast hierarchy, this could be noticeable. If there are many sprites, this can exacerbate the problem.
What you can do is have the sprite that has the focus dispatch a custom event which a controller class listens to for each sprite. The controller class will have a handler that moves the event.currentTarget however you plan to have it done.
Also read up about custom events and how to use the SystemManager to add and remove listeners dynamically: http://livedocs.adobe.com/flex/3/langref/index.html.
the most simplest example to this would be this.
here you have a controllable Ship class(Ship.as).
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.events.Event;
public class Ship extends MovieClip {
private var speedX;
private var speedY;
public function Ship() {
//constructor
stage.addEventListener(KeyboardEvent.KEY_DOWN ,keyDown);
stage.addEventListener(KeyboardEvent.KEY_UP ,keyUp);
stage.addEventListener(Event.ENTER_FRAME, update);
}
public function keyDown(e:KeyboardEvent) {
if(e.keyCode == 37) {
speedX = -5;
}
if(e.keyCode == 38) {
speedY = -5;
}
if(e.keyCode == 39) {
speedX = 5;
}
if(e.keyCode == 40) {
speedY = 5;
}
}
public function keyUp(e:KeyboardEvent) {
if(e.keyCode == 37 || e.keyCode == 39) {
speedX = 0;
}
if(e.keyCode == 38 || e.keyCode == 40) {
speedY = 0;
}
}
public function update(e:Event) {
x += speedX;
y += speedY;
}
}