Copy link to clipboard
Copied
Hello I have set up a timer in order to try to get char_mc to move when i hold in a button because i am making an ios app. I have this code
import flash.events.*;
import flash.utils.Timer;
import flash.display.MovieClip;
import flash.events.MouseEvent;
var movetimer:Timer = new Timer(50);
leftarrow.addEventListener(MouseEvent.MOUSE_DOWN, leftarrowfunctionDown);
leftarrow.addEventListener(MouseEvent.MOUSE_UP, leftarrowfunctionUp);
rightarrow.addEventListener(MouseEvent.MOUSE_DOWN, rightarrowfunctionDown);
rightarrow.addEventListener(MouseEvent.MOUSE_UP, rightarrowfunctionUp);
movetimer.addEventListener(TimerEvent.TIMER, leftarrowfunction);
movetimer.addEventListener(TimerEvent.TIMER, rightarrowfunction);
function leftarrowfunctionUp(e:MouseEvent):void
{
movetimer.stop();
}
function rightarrowfunctionUp(e:MouseEvent):void
{
movetimer.stop();
}
function leftarrowfunctionDown(e:MouseEvent):void
{
movetimer.start();
}
function rightarrowfunctionDown(e:MouseEvent):void
{
movetimer.start();
}
function leftarrowfunction(eventObject:Event):void
{
char_mc.x -=1
}
function rightarrowfunction(eventObject:Event):void
{
char_mc.x +=1
}
This should move my character one pixel every 50ms while im holding either the left or the right buttons but the character doesnt move at all. There are no errors. Any ideas? This code is on my actions layer.
You have two ooposing functions at work so you are bound not to move. The timer is calling both the right and the left arrow functions so the object would move one pixel right and one pixel left, the net effect being going nowhere.
Have just one function for the timer that tests whether the right or the left button is in use (or neither) and have that conditional dictate which way to go. Have the button functions set a boolean variable that indicates whether or not they are in use... use that v
...Copy link to clipboard
Copied
should i use touch events instead of mouse events if i want to be able to press my walk button and attack button at the same time or do multiple mouse events work at once? If so how?
Copy link to clipboard
Copied
You have two ooposing functions at work so you are bound not to move. The timer is calling both the right and the left arrow functions so the object would move one pixel right and one pixel left, the net effect being going nowhere.
Have just one function for the timer that tests whether the right or the left button is in use (or neither) and have that conditional dictate which way to go. Have the button functions set a boolean variable that indicates whether or not they are in use... use that variable in your timer conditonal.
var goRight:Boolean = false;
var goLeft:Boolean = false;
function leftarrowfunctionUp(e:MouseEvent):void
{
goLeft = false;
}
function rightarrowfunctionUp(e:MouseEvent):void
{
goRight = false;
}
function leftarrowfunctionDown(e:MouseEvent):void
{
goLeft = true;
}
function rightarrowfunctionDown(e:MouseEvent):void
{
goRight = true;
}
function timerfunction(eventObject:Event):void
{
if(goLeft) char_mc.x -=1;
if(goRight) char_mc.x +=1;
}
Copy link to clipboard
Copied
ok thanks now i have
import flash.events.*;
import flash.utils.Timer;
import flash.display.MovieClip;
import flash.events.MouseEvent;
leftarrow.buttonMode = true;
rightarrow.buttonMode = true;
var goRight:Boolean = false;
var goLeft:Boolean = false;
var movetimer:Timer = new Timer(50);
leftarrow.addEventListener(MouseEvent.MOUSE_DOWN, leftarrowfunctionDown);
leftarrow.addEventListener(MouseEvent.MOUSE_UP, leftarrowfunctionUp);
rightarrow.addEventListener(MouseEvent.MOUSE_DOWN, rightarrowfunctionDown);
rightarrow.addEventListener(MouseEvent.MOUSE_UP, rightarrowfunctionUp);
movetimer.addEventListener(TimerEvent.TIMER, timerfunction);
function leftarrowfunctionUp(e:MouseEvent):void
{
goLeft = false;
}
function rightarrowfunctionUp(e:MouseEvent):void
{
goRight = false;
}
function leftarrowfunctionDown(e:MouseEvent):void
{
goLeft = true;
}
function rightarrowfunctionDown(e:MouseEvent):void
{
goRight = true;
}
function timerfunction(eventObject:Event):void
{
if(goLeft) char_mc.x -=1;
if(goRight) char_mc.x +=1;
}
and the character still doesnt move. any ideas?
Copy link to clipboard
Copied
You still need to start the timer, but do it outside the functions.
Copy link to clipboard
Copied
Awesome thanks it works now. Also, does constant timer checks cause lag or slowness? So if i have a timer set to 5ms and moving the character 2 pixels would be a bit too smooth? Whats the recomendation? Also what fps for the app should I use? Im trying to conserve power use but still look nice. Thanks!
Copy link to clipboard
Copied
also i tried it inside the functions and it worked fine. This stops the timer when the button is released so its not constantly running so why not?
Find more inspiration, events, and resources on the new Adobe Community
Explore Now