Flash AS3 Press and Hold Button - actionscript-3

I'm no expert in Flash but I found a way in AS2 to make a "press and hold" button. Now I'm working with AS3 and I'd like this code to be converted to AS3. Can someone help ?
stop();
function startTimer(mc, conversionTime) {
mc.onEnterFrame = function() {
if ((getTimer() / 1000) - conversionTime > 1) {
delete this.onEnterFrame;
gotoAndStop(3);
}
};
}
button1.onPress = function() {
var conversionTime:Number = getTimer() / 1000;
startTimer(this, conversionTime);
this.onRelease = function() {
if (this.onEnterFrame != null) {
gotoAndStop(2);
}
delete this.onEnterFrame;
};
};
Thanks !

In AS3 it would look like this:
mc.addEventListener(MouseEvent.MOUSE_DOWN, _mouseDown);
var myTimer:Timer = new Timer(5000,1);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, _buttonPressedEnoughLong);
private function _mouseDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_UP, _mouseUp);
myTimer.start();
}
private function _mouseUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP, _mouseUp);
myTimer.reset();
}
private function _buttonPressedEnoughLong(e:TimerEvent) : void {
e.currentTarget.reset();
// Do stuff
}
You need to hold button 5 seconds, before event will fire.

Change mc.onEnterFrame = function() ... to:
mc.addEventListener(Event.ENTER_FRAME, onEvent);
function onEvent(e:Event)
{
if ((getTimer() / 1000) - conversionTime > 1)
{
this.removeEventListener(Event.ENTER_FRAME, onEvent);
gotoAndStop(3);
}
}
Change button1.onPress = function() ... to:
button1.addEventListener(MouseEvent.MOUSE_DOWN, onBtnDown);
function onBtnDown(e:MouseEvent)
{
var conversionTime:Number = getTimer() / 1000;
startTimer(this, conversionTime);
function onBtnUp(e:MouseEvent)
{
if (this.hasEventListener(Event.ENTER_FRAME))
{
gotoAndStop(2);
this.removeEventListener(Event.ENTER_FRAME, onEvent);
}
}
}

Related

Can't Stop a Gear Rotation

i am newbie in AS3 programming. I want to ask how to stop this gear animation. I just make a function to stop the rotation but the event is not going to stop.
import flash.events.MouseEvent;
var smallGearSpeed:Number = -2;
var bigGearSpeed:Number = 1;
btn_Normal.addEventListener(MouseEvent.CLICK, fl_rotateNormalHandler);
btn_Acc.addEventListener(MouseEvent.CLICK, fl_rotateAccHandler);
btn_Stop.addEventListener(MouseEvent.CLICK, fl_rotateStopHandler);
function fl_rotateStopHandler (evt:MouseEvent):void {
addEventListener(Event.ENTER_FRAME, fl_rotateStop);
}
function fl_rotateNormalHandler (evt:MouseEvent):void {
addEventListener(Event.ENTER_FRAME, fl_rotateNormal);
}
function fl_rotateAccHandler(evt:MouseEvent):void {
addEventListener(Event.ENTER_FRAME, fl_rotateAcc);
}
function fl_rotateNormal (evt:Event):void {
rotateNormal();
}
function fl_rotateAcc (evt:Event):void {
rotateAcc();
}
function fl_rotateStop (evt:Event):void {
rotateStop();
}
function rotateNormal():void {
mc_BigGear.rotation += bigGearSpeed;
mc_SmallGear.rotation += smallGearSpeed;
trace("Normal Clicked !!!");
}
function rotateAcc():void {
mc_BigGear.rotation += bigGearSpeed;
bigGearSpeed = bigGearSpeed + .1;
mc_SmallGear.rotation += smallGearSpeed;
smallGearSpeed = smallGearSpeed - .1;
trace("Accelerate Clicked !!!");
}
function rotateStop():void {
mc_BigGear.rotation = bigGearSpeed - 1;
mc_SmallGear.rotation = smallGearSpeed + 2;
trace("Stop Clicked !!!");
}
I hope someone can help me solve this problem. Sorry for my bad English , Thank you :)
To "stop" the event, remove the listener that you previously added, something like:
removeEventListener(Event.ENTER_FRAME, fl_rotateNormal);

how to use multiple times the stage event in as3

this is my code:
stop();
import gs.*;
import gs.easing.*;
import gs.TweenMax;
stage.addEventListener(MouseEvent.CLICK, rijden); // Add the button click
function rijden(e:MouseEvent):void {
TweenLite.to(auto, 4, {x:666.15, y:375.6});
}
var grav:Number = 7.5;
var jumping:Boolean = false;
var jumpPow:Number = 0;
stage.addEventListener(MouseEvent.CLICK, spring); // Add the button click
stage.addEventListener(Event.ENTER_FRAME, update);
function spring(e:MouseEvent):void {
if(jumping != true)
{
TweenLite.to(man, 0.5, {rotation:360});
jumpPow = -50;
jumping = true;
}
}
function update(evt:Event):void
{
if(jumping)
{
man.y += jumpPow;
jumpPow += grav;
if(man.y >= 375)
{
jumping = false;
man.y = 375;
}
}
}
but now if I click on the stage both the objects start to tween or something. but it needs to be like this: if I click the stage: first function rijden needs to be activated, on a second Click on the stage function spring needs to be activated.
Can someone help me??
Reformat your code like this:
//...
stage.addEventListener(MouseEvent.CLICK, rijden); // Add the button click
function rijden(e:MouseEvent):void {
stage.removeEventListener(MouseEvent.CLICK, rijden); // unplug "rijden" handler
stage.addEventListener(MouseEvent.CLICK, spring); // Add the button click
TweenLite.to(auto, 4, {x:666.15, y:375.6});
}
var grav:Number = 7.5;
var jumping:Boolean = false;
var jumpPow:Number = 0;
stage.addEventListener(Event.ENTER_FRAME, update);
function spring(e:MouseEvent):void {
stage.removeEventListener(MouseEvent.CLICK, spring); // unplug "spring" handler
if(jumping != true)
{
TweenLite.to(man, 0.5, {rotation:360});
jumpPow = -50;
jumping = true;
}
}
//...

Sprite movement basically right but

This is basically just drag and double click to set (so drag is temporarily disabled) but the sprite doesn't keep with the mouse- can someone point me to better code- otherwise I'll with go with this- so much more to do.
//The initial event performed when the button is first clicked;
internal var m_nDoubleClickSpeed:Number = 300;
internal var m_toMouse:Number;
internal var moveready:Boolean = false;
internal var initalx:uint;
internal var initialy:uint;
internal var move:Boolean;
internal function clickDoubleClick(e:MouseEvent):void {
if (isSet == false) {
this.startDrag();
}
if (isNaN(m_toMouse)==false) {
clearTimeout(m_toMouse);
HandleDoubleClick();
} else {
m_toMouse = setTimeout(HandleSingleClick, m_nDoubleClickSpeed);
}
}
internal function HandleSingleClick():void {
trace("HandleSingleClick");
trace("isSet");
trace(isSet);
this.stopDrag();
m_toMouse = NaN;
}
internal function HandleDoubleClick():void {
if (isSet == false) {
isSet = true;
this.stopDrag()
} else {
isSet = false;
}
trace("HandleDoubleClick");
m_toMouse = NaN;
}
internal function goodX(inX:Number):Number {
if (inX < 0) {
return 0;
}
if (inX > (stage.stageWidth) ) {
return (stage.stageWidth);
}
return inX;
}
internal function goodY(inY:Number):Number {
if (inY < 0) {
return 0;
}
if (inY > stage.stageHeight) {
return stage.stageHeight;
}
return inY;
}
I'm not sure if I understood you correctly. So you want to start drag on single click and drag until doubleclicked right? If so you can try something like that:
public class ClickAndDrag extends Sprite
{
private var clickTimeout:uint;
private var doubleClickSpeed:int = 500;
private var clickedOnce:Boolean;
private var mouseOnClick:Point;
private var isDragging:Boolean;
public function ClickAndDrag()
{
graphics.beginFill(Math.random()*0xffffff, 1);
graphics.drawCircle(0, 0, 20);
graphics.endFill();
this.buttonMode = true;
addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
}
private function handleMouseDown(e:MouseEvent):void
{
if (isDragging)
{
if (clickedOnce)
handleDoubleClicked();
else
{
//if it's not clicket within next doubleClickSpeed ms then doubleClickSpeed will be set to false;
clickedOnce = true;
clickTimeout = setTimeout( handleClickTimeout, doubleClickSpeed );
}
}
else
{
handleClickAndDrag();
}
}
//clicked once when dragging
private function handleClickOnce():void
{
graphics.clear();
graphics.beginFill(Math.random()*0xffffff, 1);
graphics.drawCircle(0, 0, 20);
graphics.endFill();
}
//clicked once when not dragging
private function handleClickAndDrag():void
{
isDragging = true;
this.addEventListener(Event.ENTER_FRAME, handleFrame);
mouseOnClick = new Point(this.mouseX, this.mouseY);
}
//doubleclicked when dragging
private function handleDoubleClicked():void
{
clearTimeout(clickTimeout);
clickedOnce = false;
this.removeEventListener(Event.ENTER_FRAME, handleFrame);
isDragging = false;
}
private function handleClickTimeout():void
{
clickedOnce = false;
handleClickOnce();
}
private function handleFrame(e:Event):void
{
this.x = stage.mouseX - mouseOnClick.x;
this.y = stage.mouseY - mouseOnClick.y;
}
}
It basically waits for mousedown and if it's already dragging it checks if you clicked once (changes color) ot twice (stops dragging). Or if it's not dragging yet then it starts dragging. You may also want to handle leaving the stage (Event.MOUSE_LEAVE).

to create quiz using StartDrag and stopDrag

Actually i have a quiz.fla. In the file ,two of them fill inthe blank questions and others are
multiple questions.
square1_mc must run only once not twice. İf user correct selected, doesnt run it again.
However,if mybadscoretext is 1 not increase 2,3,4. :S
how i can do all?
stop();
var myScore:Number = 0;
var myBadScore:Number=0;
square1_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);
stage.addEventListener(MouseEvent.MOUSE_UP, drop);
function drag(e:MouseEvent):void
{
e.target.startDrag();
}
function drop(e:MouseEvent):void
{
square1_mc.stopDrag();
if (square1_mc.hitTestObject(square2_mc)== true)
{
square1_mc.x=129;
square1_mc.y=133;
this.graphics.clear();
this.graphics.lineStyle(2, 000000);
this.graphics.moveTo(square1_mc.x, square1_mc.y);
this.graphics.lineTo(square2_mc.x, square2_mc.y);
this.graphics.endFill();
myScore += 1;
score_txt.text=String(myScore);
square1_mc.removeEventListener(MouseEvent.MOUSE_DOWN, drag);
}
else
{
square1_mc.x=129;
square1_mc.y=133;
myBadScore += 1;
mybadscore_txt.text=String(myBadScore);
}
}
Add a variable to keep track of whether the bad score has been added:
var badMarked:Boolean = false;
function drag(e:MouseEvent):void
{
e.target.startDrag();
badMarked = false;
}
[...]
function drop(e:MouseEvent):void
{
if (square1_mc.hitTestObject(square2_mc)== true)
{
[...]
}
else if ( !badMarked )
{
square1_mc.x=129;
square1_mc.y=133;
myBadScore += 1;
mybadscore_txt.text=String(myBadScore);
badMarked = true;
}
}

Actionscript 3 mouse_over play movie, mouse_out reverse movie

I'm trying to make some flash buttons with a mouse_over animation that plays in reverse on mouse_out. Now I have it working for one of my three movie clip instances.
I am using e.currentTarget.play() instead of having a function for each movie clip, but how do I do the same for my playReverse function? I tried putting e.currentTarget.prevFrame() instead of mc1.prevFrame() but it did not work. My code is as follows:
mc1.addEventListener(MouseEvent.MOUSE_OVER,mover);
mc2.addEventListener(MouseEvent.MOUSE_OVER,mover);
mc3.addEventListener(MouseEvent.MOUSE_OVER,mover);
mc1.addEventListener(MouseEvent.MOUSE_OUT,mout);
mc2.addEventListener(MouseEvent.MOUSE_OUT,mout);
mc3.addEventListener(MouseEvent.MOUSE_OUT,mout);
function mover(e:MouseEvent):void {
stopPlayReverse();
e.currentTarget.play();
}
function mout(e:MouseEvent):void {
this.addEventListener(Event.ENTER_FRAME, playReverse, false, 0, true);
}
function playReverse(e:Event):void {
if (mc1.currentFrame == 1) {
stopPlayReverse();
} else {
mc1.prevFrame();
}
}
function stopPlayReverse():void {
if (this.hasEventListener(Event.ENTER_FRAME)) {
this.removeEventListener(Event.ENTER_FRAME, playReverse);
}
}
Any idea how I can fix this?
Your function playReverse only use mc1 how can it work with the others movie clip.
If you choose to do it that way you can keep a track of the current MovieClip playing in reverse.
You will have to add more logic if you want that the play in reverse always finish when passing over one clip to another.
var currentMovieClip:MovieClip=null;
function mout(e:MouseEvent):void {
var mc:MovieClip = e.currentTarget as MovieClip;
if (mc !== null) {
currentMovieClip = mc;
this.addEventListener(Event.ENTER_FRAME, playReverse, false, 0, true);
}
}
function playReverse(e:Event):void {
if (currentMovieClip==null) {
return;
}
if (currentMovieClip.currentFrame == 1) {
stopPlayReverse();
} else {
currentMovieClip.prevFrame();
}
}
Another way that implies that you can have each clip to finish their play in reverse
function mover(e:MouseEvent):void {
stopPlayReverse(e.currentTarget as MovieClip);
e.currentTarget.play();
}
function mout(e:MouseEvent):void {
var mc:MovieClip = e.currentTarget as MovieClip;
if (mc !== null) {
mc.addEventListener(Event.ENTER_FRAME, playReverse, false, 0, true);
}
}
function playReverse(e:Event):void {
var mc:MovieClip = e.currentTarget as MovieClip;
if (mc.currentFrame == 1) {
stopPlayReverse(mc);
} else {
mc.prevFrame();
}
}
function stopPlayReverse(mc:MovieClip):void {
if ((mc!==null) && mc.hasEventListener(Event.ENTER_FRAME)) {
mc.removeEventListener(Event.ENTER_FRAME, playReverse);
}
}