Skip to main content
Known Participant
January 25, 2014
Answered

Timer not moving movieclip

  • January 25, 2014
  • 2 replies
  • 804 views

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.

This topic has been closed for replies.
Correct answer Ned Murphy

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;

}

2 replies

Ned Murphy
Ned MurphyCorrect answer
Legend
January 25, 2014

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;

}

Known Participant
January 26, 2014

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?

Ned Murphy
Legend
January 26, 2014

You still need to start the timer, but do it outside the functions.

Known Participant
January 25, 2014

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?