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

Timer not moving movieclip

New Here ,
Jan 25, 2014 Jan 25, 2014

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.

TOPICS
ActionScript
737
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 , Jan 25, 2014 Jan 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 v

...
Translate
New Here ,
Jan 25, 2014 Jan 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?

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 ,
Jan 25, 2014 Jan 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;

}

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 ,
Jan 26, 2014 Jan 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?

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 ,
Jan 26, 2014 Jan 26, 2014

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

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 ,
Jan 26, 2014 Jan 26, 2014

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!

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 ,
Jan 26, 2014 Jan 26, 2014
LATEST

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?

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