I need help in ActionScript3.0 - actionscript-3

I am working on a time limit game, but when I load the game and it passes the time limit that is placed it reaches zero but it is not directed to the next scene where I have the game over .. this is the code that is placed
var tiempo:int;
var Duracion:int;
Duracion = 7;
tiempo = Duracion;
var timer:Timer = new Timer(1000,Duracion);
timer.addEventListener(TimerEvent.TIMER, tiempo2);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, fin)
timer.start();
function tiempo2(tiempoevent:TimerEvent):void
{
trace(tiempo);
tiempo--;
time.text = tiempo.toString();
}
function fin(tiempoevent:TimerEvent):void
{
var timer:Timer = tiempoevent.target as Timer;
timer.removeEventListener(TimerEvent.TIMER, tiempo2)
timer.removeEventListener(TimerEvent.TIMER, fin)
}
How do I get you to the game over scene ... I'm working on animate cc

This should work
function fin(tiempoevent:TimerEvent):void
{
var timer:Timer = tiempoevent.target as Timer;
timer.removeEventListener(TimerEvent.TIMER, tiempo2);
timer.removeEventListener(TimerEvent.TIMER, fin);
nextFrame();
}

Related

I need time limit help in ActionScript3.0

I am working on a time limit game, but when I load the game and it passes the time limit that is placed it reaches zero but it is not directed to the next scene where I have the game over .. this is the code that is placed
var tiempo:int;
var Duracion:int;
Duracion = 7;
tiempo = Duracion;
var timer:Timer = new Timer(1000,Duracion);
timer.addEventListener(TimerEvent.TIMER, tiempo2);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, fin)
timer.start();
function tiempo2(tiempoevent:TimerEvent):void
{
trace(tiempo);
tiempo--;
time.text = tiempo.toString();
}
function fin(tiempoevent:TimerEvent):void
{
var timer:Timer = tiempoevent.target as Timer;
timer.removeEventListener(TimerEvent.TIMER, tiempo2)
timer.removeEventListener(TimerEvent.TIMER, fin)
}
As I do to be directed to the Scene of game over ... I am working on animate cc ...
When Timer ends, fin function will be executed.
Just add this line to go to another Scene:
function fin(tiempoevent:TimerEvent):void
{
var timer:Timer = tiempoevent.target as Timer;
timer.removeEventListener(TimerEvent.TIMER, tiempo2);
timer.removeEventListener(TimerEvent.TIMER_COMPLETE, fin);
gotoAndPlay("GameOver", 1);
}
Here is full code simplified:
var duracion:int = 7;
var timer:Timer = new Timer(1000, duracion);
timer.addEventListener(TimerEvent.TIMER, onTimerTick);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerFin);
timer.start();
function onTimerTick(event:TimerEvent):void
{
var timeLeft:int = timer.repeatCount - timer.currentCount;
time.text = String(timeLeft);
}
function onTimerFin(event:TimerEvent):void
{
timer.removeEventListener(TimerEvent.TIMER, onTimerTick);
timer.removeEventListener(TimerEvent.TIMER_COMPLETE, onTimerFin);
//go to scene named "GameOver" on frame 1
gotoAndPlay("GameOver", 1);
}

Use button to enable timer inside a function

I'm making something in actionscript 3, and when I press the first button btnSkaffPenger, it increases the number by 1 for each click. But my second button btnTrePrinter is supposed to increase the number by 1 every 2 seconds, automatically, but only works once, and doesnt reset. (I added so you can only press the button once, I don't think that interferes with the function resetting)
Thanks
The buttons code:
btnTrePrinter.addEventListener(MouseEvent.CLICK, trePrinter);
function trePrinter(evt:MouseEvent):void
{
var timer:Timer = new Timer(2000);
var harVentet:Function = function(event:TimerEvent):void{
timer.removeEventListener(TimerEvent.TIMER, harVentet);
timer = null;
sumPenger++
txtSumPenger.text = sumPenger.toString();
}
timer.addEventListener(TimerEvent.TIMER, harVentet);
timer.start();
btnTrePrinter.mouseEnabled = false;
btnTrePrinter.alpha=0.4;
}
Full code:
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
var sumPenger:int = 0;
btnSkaffPenger.addEventListener(MouseEvent.CLICK, penger1);
function penger1(evt:MouseEvent):void
{
sumPenger++
txtSumPenger.text = sumPenger.toString();
}
btnTrePrinter.addEventListener(MouseEvent.CLICK, trePrinter);
function trePrinter(evt:MouseEvent):void
{
var timer:Timer = new Timer(2000);
var harVentet:Function = function(event:TimerEvent):void{
timer.removeEventListener(TimerEvent.TIMER, harVentet);
timer = null;
sumPenger++
txtSumPenger.text = sumPenger.toString();
}
timer.addEventListener(TimerEvent.TIMER, harVentet);
timer.start();
btnTrePrinter.mouseEnabled = false;
btnTrePrinter.alpha=0.4;
}
As I was told, it's a bad practice to put the answer in comments, so I post it once again.
Just to clarify what happens in your code:
var timer:Timer = new Timer(2000);
// the timer created with 2 seconds delay and infinite repeats
var harVentet:Function = function(event:TimerEvent):void {
// 2 seconds passed after "timer.start()" call
// it's the first invocation of this listener
timer.removeEventListener(TimerEvent.TIMER, harVentet);
timer = null;
// the listener is removed and timer is destroyed
// since the listener removed from timer, no more invocations will happen
sumPenger++
txtSumPenger.text = sumPenger.toString();
}
timer.addEventListener(TimerEvent.TIMER, harVentet);
// the listener is added to timer
timer.start();
// the timer starts
Remove this code:
timer.removeEventListener(TimerEvent.TIMER, harVentet);
timer = null;
and the timer will work as you expect.

ActionScript3 remove child error

I recently have been converting an as2 fla to as3 (new to AS3) and have the entire thing working on export, but I am getting an error when I try to remove previously loaded swf's before a new swf is loaded
ArgumentError: Error #2025: The supplied DisplayObject
must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at MethodInfo-11()
I know the error relates to my removeChild code here:
`stage.addEventListener(MouseEvent.CLICK, removeSWF);
function removeSWF (e:MouseEvent):void
{
if(vBox.numChildren !=0){
// swfLoader.unloadAndStop();
vBox.removeChild(swfLoader);// empty the movieClip memory
}
}`
However, I cannot seem to find a suitable rewrite for this code that will work and not have an error. This code IS working, so I'm not sure if it would be worth my time to fix this error, or just leave it. I've already messed with it for a couple days, so at this point it's just frustrating me that I cannot fix it.
The stage mouse click listener is useful in this case because I have a back button not shown in this code that clears the loaded swf's before moving to another scene.
Does anyone see a simple solution for this, or do you think it is unnecessary to pursue since the code does what I require?
ENTIRE CODE:
function launchSWF(vBox, vFile):void {
var swfLoader:Loader = new Loader();
var swfURL:URLRequest = new URLRequest(vFile);
swfLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadProdComplete);
swfLoader.load(swfURL);
function loadProdComplete(e:Event):void {
trace("swf file loaded");
vBox.removeChild(preLoader);
vBox.addChild(swfLoader);
currentSWF = MovieClip(swfLoader.content);
currentSWF.gotoAndPlay(1);
currentSWF.addEventListener(Event.ENTER_FRAME , checkLastFrame);
swfLoader.x = 165;
swfLoader.y = 15;
function checkLastFrame(e:Event):void {
if (currentSWF.currentFrame == currentSWF.totalFrames) {
currentSWF.stop();
// trace("DONE");
}
}
}
var preLoader:loader = new loader();
preLoader.x = 450;
preLoader.y = 280;
vBox.addChild(preLoader);
function onProgressHandler(event:ProgressEvent){
var dataAmountLoaded:Number=event.bytesLoaded/event.bytesTotal*100;
//preLoader.bar.scaleX = dataAmountLoaded/100;
preLoader.lpc.text= int(dataAmountLoaded)+"%";
//trace(preLoader.bar.scaleX );
}
//NEW ERRORS BUT WORKING
stage.addEventListener(MouseEvent.CLICK, removeSWF);
function removeSWF (e:MouseEvent):void
{
if(vBox.numChildren !=0){
// swfLoader.unloadAndStop();
vBox.removeChild(swfLoader);// empty the movieClip memory
}
}
}
var container:MovieClip = new MovieClip();
var currentSWF:MovieClip = new MovieClip();
fall_b.addEventListener(MouseEvent.CLICK, fall_bClick);
function fall_bClick(e:MouseEvent):void {
var swfFile:String = 'load/fall.swf';
launchSWF(container, swfFile);
addChild(container);
}
face_b.addEventListener(MouseEvent.CLICK, face_bClick);
function face_bClick(e:MouseEvent):void {
var swfFile:String = 'load/face.swf';
launchSWF(container, swfFile);
addChild(container);
}
rott_b.addEventListener(MouseEvent.CLICK, rott_bClick);
function rott_bClick(e:MouseEvent):void {
var swfFile:String = 'load/rottgut.swf';
launchSWF(container, swfFile);
addChild(container);
}
//MORE SWFS...
Any advice anyone has is appreciated
First of all function launchSWF(vBox, vFile):void { isn't closed. You've also got function inside functions which is easy enough for you to solve if you click the lines the curly brackets start and end on to track them.
I can't see anything wrong with the code you said has an error but I'm guessing this isn't all the code. If you using Flash Professisonal you can use permit debugging to show the line the error is on.
EDIT: Please note this hasn't been tested as I'm on my mobile writing out code. That being said this should now work:
var container:MovieClip;
var currentSWF:MovieClip;
var swfFile:String;
var swfLoader:Loader;
var preLoader:Loader;
var swfURL:URLRequest;
init();
function init():void {
preLoader = new Loader();
preLoader.x = 450;
preLoader.y = 280;
vBox.addChild(preLoader);
container = new MovieClip();
currentSWF = new MovieClip();
fall_b.addEventListener(MouseEvent.CLICK, fall_bClick);
face_b.addEventListener(MouseEvent.CLICK, face_bClick);
rott_b.addEventListener(MouseEvent.CLICK, rott_bClick);
stage.addEventListener(MouseEvent.CLICK, removeSWF);
}
function launchSWF(vBox, vFile):void {
swfLoader = new Loader();
swfURL = new URLRequest(vFile);
swfLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadProdComplete);
swfLoader.load(swfURL);
}
function loadProdComplete(e:Event):void {
trace("swf file loaded");
vBox.removeChild(preLoader);
vBox.addChild(swfLoader);
currentSWF = MovieClip(swfLoader.content);
currentSWF.gotoAndPlay(1);
currentSWF.addEventListener(Event.ENTER_FRAME , checkLastFrame);
swfLoader.x = 165;
swfLoader.y = 15;
}
function checkLastFrame(e:Event):void {
if (currentSWF.currentFrame == currentSWF.totalFrames) {
currentSWF.stop();
// trace("DONE");
}
}
function onProgressHandler(event:ProgressEvent) {
var dataAmountLoaded:Number = (event.bytesLoaded / event.bytesTotal * 100);
//preLoader.bar.scaleX = dataAmountLoaded/100;
preLoader.lpc.text = int(dataAmountLoaded)+"%";
//trace(preLoader.bar.scaleX );
}
function removeSWF (e:MouseEvent):void {
if(vBox.numChildren !=0){
//swfLoader.unloadAndStop();
vBox.removeChild(swfLoader);// empty the movieClip memory
}
}
function fall_bClick(e:MouseEvent):void {
swfFile = 'load/fall.swf';
launchSWF(container, swfFile);
addChild(container);
}
function face_bClick(e:MouseEvent):void {
swfFile = 'load/face.swf';
launchSWF(container, swfFile);
addChild(container);
}
function rott_bClick(e:MouseEvent):void {
swfFile = 'load/rottgut.swf';
launchSWF(container, swfFile);
addChild(container);
}
I have this rewritten. I could not get the vBox errors cleared in the original code, and I was getting many other errors with what was posted. The vBox code was seen on a tutorial. I think it was supposed to reference the loader for the preloader and the swf, and vFile was for the actual .swf. The following code preloads multiple swfs and clears them with no errors. I appreciate your help AntBirch. I'm beginning to understand loaders in as3 a little more now.
//LOAD FIRST PIECE ON OPEN (required to removeChild later)
var swfLoader:Loader = new Loader();
var defaultSWF:URLRequest = new URLRequest("load/fall.swf");
swfLoader.load(defaultSWF);
swfLoader.x = 165;
swfLoader.y = 15;
addChild(swfLoader);
//PRELOADER
var preLoader:loader = new loader();
preLoader.x = 450;
preLoader.y = 280;
function loadProdComplete(e:Event):void {
trace("swf file loaded");
removeChild(preLoader);
addChild(swfLoader);
}
function onProgressHandler(event:ProgressEvent){
var dataAmountLoaded:Number=event.bytesLoaded/event.bytesTotal*100;
preLoader.lpc.text= int(dataAmountLoaded)+"%";
}
//BUTTONS
function btnClick(event:MouseEvent):void {
swfLoader.unloadAndStop();
removeChild(swfLoader);
swfLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadProdComplete);
addChild(preLoader);
var newSWFRequest:URLRequest = new URLRequest("load/" + event.target.name + ".swf");
swfLoader.load(newSWFRequest);
swfLoader.x = 165;
swfLoader.y = 15;;
addChild(swfLoader);
}
// BUTTON LISTENERS
fall.addEventListener(MouseEvent.CLICK, btnClick);
face.addEventListener(MouseEvent.CLICK, btnClick);
rott.addEventListener(MouseEvent.CLICK, btnClick);
angel.addEventListener(MouseEvent.CLICK, btnClick);
ratts.addEventListener(MouseEvent.CLICK, btnClick);
metal.addEventListener(MouseEvent.CLICK, btnClick);
//etc...
//BACK BUTTON
BB3.addEventListener(MouseEvent.CLICK, BB3Click);
function BB3Click(e:MouseEvent):void {
swfLoader.unloadAndStop();
removeChild(swfLoader);
this.gotoAndPlay(1 ,"Scene 2")
}

actionscript 3 sleep?

I have a simple actionscript function
var string:String = "TEXT REMOVED";
var myArray:Array = string.split("");
addEventListener(Event.ENTER_FRAME, frameLooper);
function frameLooper(event:Event):void {
if(myArray.length > 0) {
text1.appendText(myArray.shift());
}else{
removeEventListener(Event.ENTER_FRAME, frameLooper);
}
}
And I want to have it sleep after calling the framelooper so it is a little bit slower. How could I do this?
btw, I'm fairly new and found this code on a tutorial, it's a text typing effect, if there is a better way of doing this please let me know.
Use a Timer:
var string:String = "TEXT REMOVED";
var myArray:Array = string.split("");
var timer : Timer = new Timer (1000, myArray.length);
timer.addEventListener (TimerEvent.TIMER, frameLooper);
timer.start();
function frameLooper(event:Event):void {
text1.appendText(myArray.shift());
}
This will execute the frameLooper on every second for exactly as many times as the length of the array.
I'm not saying this is better than the timer method, just an option
var string:String = "TEXT REMOVED";
var myArray:Array = string.split("");
addEventListener(Event.ENTER_FRAME, frameLooper);
const WAIT_TIME:int = 10;
var i:int = 0;
function frameLooper(event:Event):void {
if(myArray.length > 0) {
if(i==0){
trace(myArray.shift());
i = WAIT_TIME;
};
} else {
removeEventListener(Event.ENTER_FRAME, frameLooper);
}
i--;
}

Controlling as2 swf playback in as3

I am embedding a swf built in flash 8 into an as3 project. When I call stop() or gotoAndStop(0); on the MovieClip that represents an instance of the embedded swf it stops for a sec and then continues. Trying to call removeChild on the mc removes it from the display but the audio in the swf keeps playing. The swf, in this case must be embedded, I cannot use loader. Any ideas
The code:
[Embed (source = "t1.swf")]
private var t1:Class;
private var mc:MovieClip;
public function iphoneTest()
{
var tf:TextField = new TextField();
tf.x = 10;
tf.y = 100;
tf.width = 100;
tf.height = 50;
tf.text = "Hello worl";
mc = new t1();
var button:CustomSimpleButton = new CustomSimpleButton();
button.width = 50;
button.height = 50;
button.x = 10;
button.y = 150;
button.addEventListener(MouseEvent.CLICK, onClick);
this.addChild(mc);
this.addChild(tf);
this.addChild(button);
}
private function onClick(e:MouseEvent):void {
mc.stop();
this.removeChild(mc);
}
did you try mc = null;?
also since you know it's an as2 swf, should probably use avm1movie instead of MovieClip
At very worst you can just kill all sounds in the SWF...
Make sure you import the sound mixer class then kill the sound..
import flash.media.SoundMixer;
SoundMixer.stopAll();
If your SWF has any hierarchy, you'll need to recurse through it to stop all movie clips.
private function stopAll(do:DisplayObject):void
{
var clip:MovieClip = do as MovieClip;
if (clip != null)
clip.stop();
var container:DisplayObjectContainer = do as DisplayObjectContainer;
if (container != null)
{
for (var i:int = 0; i < container.numChildren; ++i)
{
stopAll(container.getChildAt(i));
}
}
}