I would like to know a way can detect on how long the mouse id down on the item, and like a checker, to check on long the mouse id been down on the specific item. is it possible to using timer?
Many Thanks
Sure, but you don't need a timer:
import flash.utils.getTimer;
var startTime:int = 0;
var endTime:int = 0;
function SomeFunctionOrClassConstructor():void
{
item.addEventListener(MouseEvent.MOUSE_DOWN, startMouseDown, false, 0, true);
}
function startMouseDown($evt:MouseEvent):void {
startTime = getTimer();
item.removeEventListener(MouseEvent.MOUSE_DOWN, startMouseDown);
item.addEventListener(MouseEvent.MOUSE_UP, endMouseDown, false, 0, true);
}
function endMouseDown($evt:MouseEvent):void {
endTime = getTimer();
item.addEventListener(MouseEvent.MOUSE_DOWN, startMouseDown, false, 0, true);
item.removeEventListener(MouseEvent.MOUSE_UP, endMouseDown);
trace(endTime - startTime);
}
That should get you started.
Related
this seems like an odd one as it seems pretty straight forward, I have a movieclip that contains a back and forward button to invoke a tween for 2 groups of buttons to slide accross the screenI addChild then make the tween and then removeChild for the old group of buttons to leave the new one on the screen.
My question is when the second group comes in with all the buttons the button that is on the top layer of the movieclip fails to work (rollOver Tween and all) if I change which button is on the top layer then that one fails.
any ideas as to why this may occur so I can make a fix?
code for process is below.
import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import fl.transitions.easing.Strong;
import flash.events.*;
import flash.display.MovieClip;
fOverlay.addEventListener(MouseEvent.ROLL_OVER, mOver);
fOverlay.addEventListener(MouseEvent.ROLL_OUT, mOut);
fOverlay.addEventListener(MouseEvent.CLICK, mDown);
bOverlay.addEventListener(MouseEvent.ROLL_OVER, mOverB);
bOverlay.addEventListener(MouseEvent.ROLL_OUT, mOutB);
bOverlay.addEventListener(MouseEvent.CLICK, mDownB);
var butts1:MovieClip = new sButtons1;
var butts2:MovieClip = new sButtons2;
addChild(butts1);
butts1.x = -10;
butts1.y = 0;
function mOver(e:MouseEvent)
{
var mouseOTween:Tween = new Tween(fBack, "alpha", Strong.easeOut,0.2,0.8,1,true);
}
function mOut(e:MouseEvent)
{
var mouseOTween:Tween = new Tween(fBack, "alpha", Strong.easeOut,0.8,0.2,1,true);
}
function mOverB(e:MouseEvent)
{
var mouseOTween:Tween = new Tween(bBack, "alpha", Strong.easeOut,0.2,0.8,1,true);
}
function mOutB(e:MouseEvent)
{
var mouseOTween:Tween = new Tween(bBack, "alpha", Strong.easeOut,0.8,0.2,1,true);
}
function mDown(e:MouseEvent)
{
if (butts1.stage)
{
addChild(butts2)
butts2.x = 1832;
butts2.y = 0;
var nTweenout1:Tween = new Tween(butts1,"x", Strong.easeInOut, -10, -1860, 1, true);
var nTweenin1:Tween = new Tween(butts2,"x", Strong.easeInOut, 1832, -10, 1, true);
nTweenout1.addEventListener(TweenEvent.MOTION_FINISH, moFinish1);
function moFinish1(e:TweenEvent)
{
removeChild(butts1);
}
}
else if (butts2.stage)
{
addChild(butts1)
butts1.x = 1832;
butts1.y = 0;
var nTweenout2:Tween = new Tween(butts2,"x", Strong.easeInOut, -10, -1860, 1, true);
var nTweenin2:Tween = new Tween(butts1,"x", Strong.easeInOut, 1832, -10, 1, true);
nTweenout2.addEventListener(TweenEvent.MOTION_FINISH, moFinish2);
function moFinish2(e:TweenEvent)
{
removeChild(butts2);
}
}
else
{
MovieClip(root).addChild(butts1)
butts1.x = -10;
butts1.y = 0;
}
}
function mDownB(e:MouseEvent)
{
if (butts1.stage)
{
addChild(butts2)
butts2.x = -1860;
butts2.y = 0;
var nTweenoutB1:Tween = new Tween(butts2,"x", Strong.easeInOut, -1860, -10, 1, true);
var nTweeninB1:Tween = new Tween(butts1,"x", Strong.easeInOut, -10, 1832, 1, true);
nTweenoutB1.addEventListener(TweenEvent.MOTION_FINISH, moFinish3);
function moFinish3(e:TweenEvent)
{
removeChild(butts1);
}
}
else if (butts2.stage)
{
addChild(butts1)
butts1.x = -1860;
butts1.y = 0;
var nTweenoutB2:Tween = new Tween(butts1,"x", Strong.easeInOut, -1860, -10, 1, true);
var nTweeninB2:Tween = new Tween(butts2,"x", Strong.easeInOut, -10, 1832, 1, true);
nTweenoutB2.addEventListener(TweenEvent.MOTION_FINISH, moFinish4);
function moFinish4(e:TweenEvent)
{
removeChild(butts2);
}
}
else
{
addChild(butts1)
butts1.x = -10;
butts1.y = 0;
}
}
I suspect you've got objects overlapping eachother. In that case, only the top-most object will receive the mouse event. If so, it is expected behavior. An example of this can be seen with a transparent png.
Without the rest of your DisplayList structure or appearance of your UI, I can't say exactly what's plaguing your events. However, I think the code below might help clear up some things.
import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import fl.transitions.easing.Strong;
import flash.events.*;
import flash.display.MovieClip;
var butts1:MovieClip = new sButtons1;
var butts2:MovieClip = new sButtons2;
var lookup:Dictionary = new Dictionary(true);
var removeTarget:MovieClip = null; // This is the next object to be removed from onscreen
var onComplete:Tween = null; // A reference to the tween the calls our remove
function init():void {
// It's healthy to keep your initialization routine grouped away from the rest of your program logic.
// Optionally, you can recall the function to rerun first time setup, if necessary.
addChild(butts1);
butts1.x = -10;
butts1.y = 0;
// Use event target as a key in our dictionary to correspond which objects should be hidden/shown
// This way, we can keep our code consistent and maintainable from one function.
lookup[fOverlay] = {
"back":fBack,
"out":butts1,
"in":butts2
}
lookup[bOverlay] = {
"back":bBack,
"out":butts2,
"in":butts1
}
// Because we're registering the same events to the same functions, we can handle it through a loop.
var overlayEvents:Array = ["rollOver", "rollOut", "click"];
for (var overlay in lookup) {
for each (var type:String in overlayEvents) {
overlay.addEventListener(type, overlayListener, false, 0, true);
}
}
}
function overlayListener(e:MouseEvent):void {
// Having generalized our objects, we swap for the appropriate object based on the type of event.
switch (e.type) {
case "rollOver":
new Tween(lookup[e.currentTarget].back, "alpha", Strong.easeOut,0.2,0.8,1,true);
break;
case "rollOut":
new Tween(lookup[e.currentTarget].back, "alpha", Strong.easeOut,0.8,0.2,1,true);
break;
case "click":
var show:MovieClip, hide:MovieClip;
if (lookup[e.currentTarget].out.stage) {
hide = lookup[e.currentTarget].out;
show = lookup[e.currentTarget].in;
} else if (lookup[e.currentTarget].in.stage) {
hide = lookup[e.currentTarget].in;
show = lookup[e.currentTarget].out;
} else {
show = butts1;
addChild(show);
}
show.x = 1832;
show.y = 0;
new Tween(hide, "x", Strong.easeInOut, -10, -1860, 1, true);
if (hide != null) {
removeTarget = hide;
if (onComplete != null) {
onComplete.stop()
onComplete.rewind()
}
onComplete = new Tween(show, "x", Strong.easeInOut, 1832, -10, 1, true);
onComplete.addEventListener("motionFinish", remove, false, 0, true);
}
break;
}
}
function remove(e:Event):void {
if (removeTarget != null) {
removeChild(removeTarget)
removeTarget = null;
onComplete.removeEventListener("motionFinish", remove);
onComplete = null;
}
}
init();
So basically I want to make a Blinking effect with a textField where the time will be 1 sec. I have only a "brut" code that I think it can be done easier but coundt figure out how to make it loop.
i have only this
private var myBlackText:TextField = new TextField();
private var myRedText:TextField = new TextField();
private var format:TextFormat = new TextFormat();
public function Main()
{
this.addChild(myBlackText)
myBlackText.defaultTextFormat = new TextFormat('Verdana',20,0x000000);
myBlackText.x = 200
myBlackText.y = 200
myBlackText.text = "YOYO"
this.addChild(myRedText)
myRedText.defaultTextFormat = new TextFormat('Verdana',20,0xFF0000);
myRedText.x = 200
myRedText.y = 200
myRedText.text = "YOYO"
TweenLite.to( myRedText, 1, { alpha:0, onComplete:ShowRed });
function ShowRed():void
{
TweenLite.to( myRedText, 1, { alpha:1, onComplete:HideRed });
}
function HideRed():void
{
TweenLite.to( myRedText, 1, { alpha:0, onComplete:ShowRed });
}
stage.addEventListener(MouseEvent.CLICK, onClick);
}
private function onClick(ev:MouseEvent):void
{
//how do I stop the TweenLite ????
}
If you only want to kill the tween, this is as simple as possible.
TweenLite.killTweensOf(myRedText);//will kill all tweens of myRedText
Try this if you want it be more simple in just on line.
TweenMax.to( myRedText, 1, {alpha:0, repeat:-1, yoyo:true} );
Explain:
repeat=-1 means repeat forever.
yoyo=true means do Red's alpha from 1-0 and 0-1
So the whole is Red's alpha from 1-0-1-0-1...
I want to set a duration of time by hitting space bar twice. I then want a movieclip to play for that exact amount of time, then loop to play again at for that set amount of time, and so on. until I set a different amount of time by hitting the space bar twice again.
var beat:int;
var beatcount:int;
var tempopress:int;
var num:Number;
num = 0;
tempopress = 0;
stage.addEventListener(KeyboardEvent.KEY_DOWN,checker);
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
var myTimer:Timer=new Timer(20,0);
myTimer.addEventListener(TimerEvent.TIMER, stopWatch);
function stopWatch(event:TimerEvent):void {
beatcount = Number(myTimer.currentCount);
}
function checker(e:KeyboardEvent){
if(e.keyCode==Keyboard.SPACE){
if (tempopress == 0) {
trace('start');
beatcount = 0;
myTimer.reset();
myTimer.start();
tempopress = 1;
} else {
trace('stop');
myTimer.stop();
trace(beatcount);
tempopress = 0;
}
}
}
stage.addEventListener(Event.ENTER_FRAME, loopPlayback);
function loopPlayback() {
var loopTimer:Timer=new Timer(20,beatcount);
myTimer.addEventListener(TimerEvent.TIMER, loopWatch);
}
function loopWatch(event:TimerEvent):void {
if (MovieClipMan.currentFrame >= MovieClipMan.totalFrames ){
MovieClipMan.gotoAndStop(1);
} else {
MovieClipMan.nextFrame();
}
}
I know it's a mess haha. Please help! :]
I'd perhaps try something like this, which essentially is checking to see whether to do the loop or not each frame.
var timeStart:Number;
var loopDuration:Number;
var timeLastLoop:Number;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onKeyDown(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.SPACE) {
if (!timeStart) { // First time SPACE is hit
timeStart = getTimer();
} else { // Second time SPACE is hit
loopDuration = getTimer() - timeStart; // set the loop duration
timeStart = NaN; // reset the start time
loop();
}
}
}
function onEnterFrame(e:Event):void {
if (loopDuration && timeLastLoop) {
if (getTimer() >= timeLastLoop + loopDuration) { // if it's time to loop
loop();
}
}
}
function loop():void {
timeLastLoop = getTimer();
someMovieClip_mc.gotoAndPlay(0);
}
First, use getTimer() to find the difference in time between space bar keypress.
Next, would be to stop creating a new Timer in every frame. It should be created outside of the enter frame handler. Then on the second keypress, you can set the delay property to the difference, and restart the timer.
The most important changes would be here:
if (tempopress == 0) {
trace('start');
myTimer.stop();
startTime = getTimer();
beatcount = 0;
tempopress = 1;
} else {
trace('stop');
myTimer.delay = getTimer() - startTime;
myTimer.reset();
myTimer.start();
tempopress = 0;
}
Then, the timer event handler can just send the MovieClip to frame 1.
If there is some event on any button such as rollover event by mouse how to measure how much time that rollover event took place?
You could time the difference between MOUSE_OVER and MOUSE_OUT.
var myButton:Button = new Button();
var diff:Number = 0;
var startDate:Date;
myButton.addEventListener(MouseEvent.MOUSE_OVER, function(evt:MouseEvent):void {
startDate = new Date();
});
myButton.addEventListener(MouseEvent.MOUSE_OUT, function(evt:MouseEvent):void {
diff = (new Date()).time - startDate.time;
if (diff >= 5000)
// do something
});
I do not have Flash Builder up but this should be a good start. Check out these docs for more info:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/MouseEvent.html
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Date.html#time
Well, only one rollover is fired unless yuo roll out and then back on.
So here's what I would do:
private var timeoutId:uint=-1;
private function onRollover(e:MouseEvent):void {
timeoutId=setTimeout(play, 5000);
}
private function onRollout(e:MouseEvent):void {
if(timeoutId != -1) clearTimeout(timeoutId);
}
private function play():void {
//code to play music
}
and of course, onRollover and onRollout handle the respective events.
Maybe you should take a look at the Timer class. Here's a simple example of what you can do with it:
var timer:Timer = new Timer(5000,1);
timer.addEventListener(TimerEvent.TIMER, playVideo);
btnInstance.addEventListener(MouseEvent.MOUSE_OVER,
function(evt:MouseEvent):void {
timer.start();
});
btnInstance.addEventListener(MouseEvent.MOUSE_OUT,
function(evt:MouseEvent):void {
timer.stop();
});
function playVideo(evt:TimerEvent):void {
// play video
}
Hope it helps.
Hello
I am in the process of creating a chess board where you can move the pieces. Currently i am working on the rook and the coding is below. I know it is not elegant and probably the most inefficient code out there, but this is day 2 of my actionscript 3.0 life and i am kinda a beginner. Anyway, so the thing is, when you click the piece the code below figures out the possible ways to go. Then green squares appear at those places. You can then press those green squares and then the rook will move there.
Ok, now to the problem. The squares will not go away. I want them all to be deleted when i have clicked on one of them and the rook will move there.
I have tried removeChild(), but since that happens in a different function it does not work. So if you are so kind to look through the code and suggest a solution, your help is much appreciated.
Kind Regards Emile
https://picasaweb.google.com/109156245246626370734/Jun42011?authkey=Gv1sRgCMy4v_b01aikzAE&feat=directlink
import flash.display.Sprite
import flash.events.MouseEvent
import flash.text.TextField;
import flash.geom.Point;
import caurina.transitions.*
myPoint.addEventListener(MouseEvent.MOUSE_DOWN, startMove);
function startMove(evt:MouseEvent) {
var boxNum:int = Math.floor(myPoint.y/100)+1;
for (var i:int = 1; i <boxNum; i++) {
var box:Ball = new Ball();
box.x = myPoint.x;
box.y = myPoint.y - i * box.height;
addChild(box);
Tweener.addTween(box, {alpha:0.5});
box.buttonMode = true;
box.addEventListener(MouseEvent.ROLL_OVER, onOver,
false, 0, true);
box.addEventListener(MouseEvent.ROLL_OUT, onOut,
false, 0, true);
box.addEventListener(MouseEvent.MOUSE_DOWN, onclick);
}
var boxNum1:int = Math.floor((800-myPoint.y)/100)+1;
for (var i:int = 1; i <boxNum1; i++) {
var box1:Ball = new Ball();
box1.x = myPoint.x;
box1.y = myPoint.y + i * box.height;
addChild(box1);
Tweener.addTween(box1, {alpha:0.5});
box1.buttonMode = true;
box1.addEventListener(MouseEvent.ROLL_OVER, onOver,
false, 0, true);
box1.addEventListener(MouseEvent.ROLL_OUT, onOut,
false, 0, true);
box1.addEventListener(MouseEvent.CLICK, onclick);
}
var boxNum2:int = Math.floor(myPoint.x/100)+1;
for (var i:int = 1; i <boxNum2; i++) {
var box2:Ball = new Ball();
box2.x = myPoint.x - i * box.height;
box2.y = myPoint.y;
addChild(box2);
Tweener.addTween(box2, {alpha:0.5});
box2.buttonMode = true;
box2.addEventListener(MouseEvent.ROLL_OVER, onOver,
false, 0, true);
box2.addEventListener(MouseEvent.ROLL_OUT, onOut,
false, 0, true);
box2.addEventListener(MouseEvent.CLICK, onclick);
}
var boxNum3:int = Math.floor((800-myPoint.x)/100)+1;
for (var i:int = 1; i <boxNum3; i++) {
var box3:Ball = new Ball();
box3.x = myPoint.x + i * box.height;
box3.y = myPoint.y;
addChild(box3);
Tweener.addTween(box3, {alpha:0.5});
box3.buttonMode = true;
box3.addEventListener(MouseEvent.ROLL_OVER, onOver, false, 0, true);
box3.addEventListener(MouseEvent.ROLL_OUT, onOut, false, 0, true);
box3.addEventListener(MouseEvent.CLICK, onclick);
}
}
function onOver(evt:Event):void {
var box:MovieClip = MovieClip(evt.target);
addChild(box)
box.scaleX = box.scaleY = 1.1;
}
function onOut(evt:Event):void {
evt.target.scaleX = evt.target.scaleY = 1;
}
function onclick(Event:MouseEvent):void {
var xcod:int = Math.ceil(mouseX/100)*100-50;
var ycod:int = Math.ceil(mouseY/100)*100-50;
Tweener.addTween(myPoint, {x:xcod, y:ycod, time:1, transition:"linear"});
}
alxx's answer is correct, you wouldn't need to keep a special list for them. The other way you could do it, using an Array to save references, would look like this:
var boxes:Array = new Array();
function startMove(evt:MouseEvent):void {
...
var box:Ball = new Ball();
addChild(box);
boxes.push(box);
...
var box1:Ball = new Ball();
addChild(box1);
boxes.push(box1);
...
}
function onClick(evt:MouseEvent):void {
for each (var box:Ball in boxes) {
removeChild(box);
}
boxes = new Array();
}
You can put temporary highlights in separate Sprite. Then your board will looks as follows:
Stage children: base board, highlights, pieces, in that order.
When you need to remove highlights, you can iterate highlights' children with numChildren and getChildAt and call removeChild on each, you don't even need a special list for them.