Trigger a frame update in Flash - actionscript-3

I've encountered a case where I'm calling code that causes a flicker in a bitmap data. Without changing the frame rate to a much higher value (don't know if this is possible to change dynamically at runtime) is it possible to redraw the frame quickly?
In the old days you could cause Flash Player to manually update the frame by calling updateAfterEvent(). Does this still work? Is there another way to update the frame?
Here is what I have so far:
// force redraw
var updateEvent:MouseEvent = new MouseEvent(MouseEvent.MOUSE_MOVE);
updateEvent.updateAfterEvent();
If it's local variables will this get garbage collected?

If you're changing a something during enter frame listener, it should get updated automatically. Otherwise, use updateAfterEvent() in a non-enterframe listener.
function onMouseMove(e:MouseEvent):void {
// ... code that changes something
e.updateAfterEvent();
}

Related

Flash CS6 Button Not Working Despite No Compiler/Output Errors

I'm using Flash CS6, AS3 to create buttons for my project. Below is my code:
Intro_btn.addEventListener(MouseEvent.MOUSE_DOWN, Intro_func);
function Intro_func(event:MouseEvent):void {
gotoAndStop("Intro");
}
No errors appear when I run it on the output and compiler panel, and the same happens when I run it through the debugger. Also, I have used the exact same code for five other buttons, and they have no problems working. Please can someone tell me what is wrong with my code???
I would need to know more info of your code and the type of buttons you're using, however I would try this steps:
1.- add a Button component from the component inspector, set its instance name, and add that same function to the Click event:
newButton.addEventListener(MouseEvent.CLICK, Intro_func);
function Intro_func(event:MouseEvent):void {
trace("function executed");
gotoAndStop("Intro");
}
2.- run your app and test if the action is executed with this new button. If this test pass, then your problem is your button.
check this on your button:
check the base class of the button (be sure its a MovieClip or a Button class)
check if your button is enabled/disabled.
check if your button or its children has mouseEnabled and mouseChildren set as
true.
check there is no other object on top of the button either in your artboard or added by code at runtime.
I finally would suggest you to set useHandCursor = true; this will replace your mouse pointer arrow to a hand when you move over your button (this is just to test that your button is actually interacting with mouse).
hope this helps.
sorry for the delay, But I've found your solution, It's easier than you thought: your frame is delayed.
your Intro_btn script is on frame 10, however in frame one you're telling it to go to frame 9 (the "Erhu" frame label) so the actions to setup the event listener are never being called.
Also keep in mind the following:
objects exists only in their frames life time, if you have an object from frame 5 to 10, it will only exist in there, so moving to a previous or later frame (e.g. frames 4 and 11) will internally delete the object in memory along with their asociated actions, in english:
you placed the button in frame 10 and added its MOUSE_DOWN listener however, if you go back to frame 0, since the button doesn't exists in this frame it will be deleted from memory along with its listeners, so if you go from frame 0 to any other frame different than 10, your button will never have its listeners associated.
So my suggestion:
1.- add your function into frame 0:
function Intro_func(event:MouseEvent):void {
trace("function executed");
gotoAndStop("Intro");
}
2.- create a new layer. In this layer add a keyframe at the same position where your Intro_btn is(frame 10), and fill the rest of the timeline of this layer with empty frames (no keyframes), finally in the same layer add in frame 10 your listener Intro_btn.addEventListener. this way, the action is available for every subsecuent frame from frame 10.
hope this solves your problem.

xml frame pause flash

So is it posibble to create outside source from swf file so i can control frame pause lenght(xml file or AS pacage), and edit it with notepad. I have this code for each of my frames in swf file it goes like this:
Code on my first frame goes like this:
//PAUSE
function playit(e:TimerEvent):void {
play();
}
var Tim:Timer = new Timer(100, 1);
Tim.addEventListener(TimerEvent.TIMER, playit);
stop();
Tim.delay = 100; //Adjust Accordingly - 1000 Equals 1 Second
Tim.start();
And on the rest of the frames (10 more frames) goes like this:
//PAUSE
stop();
Tim.delay = 10000; //Adjust Accordingly - 1000 Equals 1 Second
Tim.start();
Sorry to say it, but your code is a bit messy. If you are actually trying to set how much frames should be played per second (as I can see the timer), you should check frameRate property. You can read specific number from a source file and then set it with ActionScript.
You won't need all those play/stop/timer things..
Hope that's the idea, otherwise I'm speechless..
p.s.
If you still need some other kind of solution - use nextFrame/prevFrame with that timer, don't play/stop it all the time..
The solution is simple but require a bit of math. Instead of letting the movie play you need to use your own custom system based on gotoAndStop(). You calculate the right timing and then call gotoAndStop() at the right moment with a variable of int type increasing on each call.

Actionscript to play/pause audio on different buttons

I've created a few buttons in Flash. I'm trying to make it so that if you click one button, the audio starts playing for that button. If you click another button, the active audio stops and the new audio of the button you clicked last start playing.
Any help please?
What you're describing is actually quite easy to do.
First things first, I recommend importing the audio into your Flash project. Alternatively, there is a way to play it directly from an external file. This is beyond the scope of my answer, so if you need help on that, you should post a question specifically covering it.
Assuming you have imported the audio file into your Flash project's library, make an as3 instance of it. (Right click the file in the library, click Properties --> ActionScript [tab] --> [Check] Export for ActionScript & [Enter name in] Class)
Now, create a definition of the sound in your code. (Assuming your two sounds were named "mySound1" and "mySound2" in the Class field of the previous step.)
var mySound1:Sound = new mySound1();
var mySound2:Sound = new mySound2();
Now, define your sound channel.
var mySoundChannel:SoundChannel = new SoundChannel();
There are two alternate ways of stopping one sound and playing another. The first is to create one function that does both every time. The second method is to create two formulas, one for "play" and one for "stop". You will need to decide which method works best for you. I'll use the two-function method below:
function stopSound():void
{
//This stops all sound in the sound channel.
//If there is nothing playing, nothing happens.
mySoundChannel.stop();
}
//In this function, we create an argument that allows us to tell the function
//what sound to we want it to play.
function playSound(soundname:String):void
{
mySoundChannel = this[soundname].play(0, 0);
}
[Note, you can tweak the play() properties to meet your needs, doing things like starting in the middle of the song, or looping it forever. 0,0 starts at the beginning, and doesn't loop. See the documentation for this.]
Now you hook up the event listeners for the buttons. (If you need help with event listeners, read the documentation.)
myButton1.addEventListener(Mouse.CLICK, btn1Click);
myButton2.addEventListener(Mouse.CLICK, btn2Click);
function btn1Click(evt:Event):void
{
stopSound();
playSound(mySound1);
}
function btn2Click(evt:Event):void
{
stopSound();
playSound(mySound2);
}
This should be enough information to get you started. In my game core, I actually have a custom class for dealing with sound playback that gives me the ability to repeat sounds, change volume, and keep sounds from conflicting with each other. I say that to emphasize that you can do quite a bit with the sound class. Do some digging in that documentation for ideas and help.
You may also consider putting a try-catch statement in the playSound function, since it will throw an reference error if you pass a name for a sound that doesn't exist.

Tweening in as3 stopping the next tween to happen until the last one is finished

How to stop the next tween action to start until the previous tween completes playing in as3.0?
and also i want to stop the tween to happen on the same object twice.
Basically i have a container (movie-clip) in which there are n number of movie-clips (arranged as bricks). When i click on the container the target (brick) will disappear (made scaleX and alpha to 'o'). also i am tracking how many bricks are closed.
But the problem is if i do a fast double click the tween seems to happen twice. and the count also seems to increase for the same brick. how to solve this problem.
Tween after Tween
Use an event handler to wait for the first tween to complete, Something like
myTween.addEventListener(TweenEvent.MOTION_FINISH, onFinish);
function onFinish(e:TweenEvent):void {
... // Call the next tween here
}
Double click problem,
Remove the event listener for the button click as soon as it is clicked, Something like:
my_btn.addEventListener(MouseEvent.CLICK, onClick);
function onClick(e:MouseEvent){
my_btn.removeEventListener(MouseEvent.CLICK, onClick);
myTween.start();
}
You could use Tweener and use
isTweening() or autoOverwrite = false to get around this.
I haven't used those properties/functions in a while but I'm sure something in Tweener can do this easily.
Any reason you are using Tweener. Greensock's Tweenmax/lite package is seemingly the industry standard these days.
The problem you seem to be having is that you are initiating the tween multiple times. If all the blocks need to shrink simultaneously, i would probably use a for loop and run a tween on each of them.
The thing you have to make sure though is once its clicked to remove the click event handler until its complete so it won't be clicked again.
I also would recommend using TweenMax/Lite because it has something called TimelineMax/Lite where you can compound a bunch of tween be it simultaneous or staggered and listen for one COMPLETE event on that Timeline object. When that event fires you can reactivate the clicks or do whatever it is you need to do after the animation is complete.
TimelineMax has some nice features as well like reverse and timeScale.
would you be able to paste some code? that would also help us help you.

hitTestObject, stopDrag stops drag on two movieclips even though function states one movieclip to stop drag

I have a function that states when movieclip1 is dragged and hits a line then it stops the drag, however it seems to stop the entire drag function in the swf on the other movieclips even though they arent called in the function. Can somebody please help me with this.
Regards
T
Here is the code:
function hitTest(event:Event):void
{
if (movieclip1.hitTestObject(line))
{
movieclip1.stopDrag();
}
else
{
}
}
Are you absolutely positive you only have one instance of movieclip1 on your stage? Definitely double check. Are you creating them dynamically, or are they preloaded when your SWF loads?
If they're preloaded:
Perhaps during testing you made some quick copies of it, and now those copies have the same name and they're all responding the same. That's my first guess.
If they're loaded dynamically:
Check the function where they're being created. If you're naming them in a loop (with a number on the end like the above), be sure that you're properly increasing the numeric value used on the end.