PDA

View Full Version : moving a mc with mouseover and timers



chopficaro
February 23rd, 2010, 06:33 PM
this code is supposed to, on mouse over, move a mc 50 pixels to the right in 10 frames, and on mouse out, move it back. im also trying to take into consideration what happenes if the mouse event occurs during the movement, but im seeing some very strange behavior, draw a block on the stage and make it a movie clip and ull see what i mean. sometimes the block moves 5 pixels and stops, other times it moves untill it leaves the stage.


package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class FunButton extends MovieClip
{
var destinationX:Number = new Number();
var origX:Number = new Number();
var moveLeftTimer:Timer = new Timer(0,0);
var moveRightTimer:Timer = new Timer(0,0);
public function FunButton()
{
var destinationX:Number = this.x + 50;
var origX:Number = this.x;
this.addEventListener(MouseEvent.MOUSE_OVER, TimerRight);
this.addEventListener(MouseEvent.MOUSE_OUT, TimerLeft);
}

private function TimerRight(event:MouseEvent):void
{
var numTimer:Number = int((destinationX - this.x)/5);
var moveRightTimer = new Timer(50,numTimer);
if(moveLeftTimer.running){ moveLeftTimer.stop(); }
if(moveRightTimer.running){ moveRightTimer.stop(); }
moveRightTimer.addEventListener(TimerEvent.TIMER, MoveRight);
moveRightTimer.start();

}

private function TimerLeft(event:MouseEvent):void
{
var numTimer:Number = int((this.x - origX)/5);
var moveLeftTimer = new Timer(50,numTimer);
if(moveLeftTimer.running){ moveLeftTimer.stop(); }
if(moveRightTimer.running){ moveRightTimer.stop(); }
moveLeftTimer.addEventListener(TimerEvent.TIMER, MoveLeft);
moveLeftTimer.start();
}

private function MoveRight(event:TimerEvent):void
{
this.x += 5;
}

private function MoveLeft(event:TimerEvent):void
{
this.x -= 5;
}
}
}

creatify
February 23rd, 2010, 09:23 PM
To be honest, you are using about 90% more code than you need to to achieve this movement? I think that's mainly why no one has replied to your thread. If you look into TweenMax (http://www.greensock.com/tweenmax/) you're class would look like this:


package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import com.greensock.TweenMax;

public class FunButton extends MovieClip {

var destinationX:Number;
var origX:Number;
var speed:Number = 0.5; //in seconds

public function FunButton() {
origX - this.x;
destinationX = origX+50;
this.addEventListener(MouseEvent.MOUSE_OVER, moveRight);
this.addEventListener(MouseEvent.MOUSE_OUT, moveLeft);
}

private function moveRight(event:MouseEvent):void {
TweenMax.to(this, speed, {x:destinationX, overwrite:true});
}

private function moveLeft(event:MouseEvent):void {
TweenMax.to(this, speed, {x:origX, overwrite:true});
}
}
}


You're also declaring Class properties (up top) but are declaring the same-named properties within your functions, so without very close review, I can't explain what's going on - basically it appears to me you're just piling up Timer instance after Timer instance after Timer instance and they're all firing the same function.

If for some reason you're trying to avoid using a tween engine, then I'd look into using an single ENTER_FRAME event instead of timers, there are various reasons for this, but thats for another day :)

Hope this helps.

snickelfritz
February 24th, 2010, 03:20 AM
You need to create an invisible hit area (a Shape with alpha set to 0) within the button (MovieClip that is...)
Then create the visible part of the button as a MovieClip within the button.
Move the nested movieclip, not the button itself.
Use TweenLite as suggested by creatify.
You probably don't need the origX and destX variables, just use x:50 for the rollover and x:0 for the rollout.
The invisible hit area will prevent the induced mouse events from occurring.

chopficaro
February 24th, 2010, 04:54 AM
ty, i will investigate tween max, but truthfully this question has beeen simplified, i am trying to create an easing motion and i needed to get this to work before i could ease it. if tweenmax cant do what i need it to do then i will take snick's suggestion

snickelfritz
February 24th, 2010, 06:02 AM
TweenMax has a full suite of easing solutions, as well as custom easing tool for creating your own custom easing.
You can even simulate Timeline functionality with TimelineLite.
http://blog.greensock.com/timeline-basics/

chopficaro
February 24th, 2010, 09:09 AM
ty, very nice, ive got some reading to do

chopficaro
February 28th, 2010, 07:00 PM
i just spent the last few days without power, so i went to the library and printed out a bunch of greensock documentation and spent the whole blackout reading it with a flashlight.

gotta say I LOVE IT! everything ive studied in flash so far is 470382 times better with greensock's tweeners and timelines!

thanks guys so much for pointing me in that direction. do u have any other recommendations for important external libraries?

snickelfritz
February 28th, 2010, 07:22 PM
papervision3d, for 3D projects.
swfObject, for embedding the SWF in HTML; This is an absolute must-have.
swfAddress, for creating browser history, like a traditional HTML page.