use of button to increase and decrease the timer in action script3.0 - actionscript-3

I have written a code to add two buttons. It shows them correctly, but the action is not performed correctly.
public class butt extends Sprite {
public function butt() {
var delayGlobal:Number = 2000;
var min1:Number =1000;
var myTimer:Timer = new Timer(delayGlobal);
myTimer.addEventListener(TimerEvent.TIMER,runMany);
myTimer.start();
// Button Event
myButton1.addEventListener(MouseEvent.CLICK, myButton1Click);
function myButton1Click(ev:MouseEvent):void {
delayGlobal = delayGlobal- 1000;
trace(delayGlobal);
}
myButton2.addEventListener(MouseEvent.CLICK, myButton2Click);
function myButton2Click(ev:MouseEvent):void {
delayGlobal = delayGlobal + 1000;
trace(delayGlobal);
}
function runMany(e:TimerEvent):void {
var loader:Loader=new Loader();
var url:String= "http://google.com.example2";
loader.load(new URLRequest(url));
addChild(loader);
}
}
}
The timer is shown but doesn't work

Your issue is that you are updating a variable, but not actually updating the timer at all.
You need to explicitly tell the timer to change the delay. myTimer.delay = delayGlobal
Here is a re-working of your code:
public class butt extends Sprite {
private var min1:Number =1000; //assuming this is your minimum allowed delay?
private var myTimer:Timer;
public function butt() {
myTimer = new Timer(2000);
myTimer.addEventListener(TimerEvent.TIMER,runMany);
myTimer.start();
// Button Event
myButton1.addEventListener(MouseEvent.CLICK, myButton1Click);
myButton2.addEventListener(MouseEvent.CLICK, myButton2Click);
}
private function myButton1Click(ev:MouseEvent):void {
myTimer.delay = Math.max(min1, myTimer.delay - 1000); //This will assign whatever is bigger, min1 or the timer delay less 1000 - ensuring that the timer doesn't drop below the value of min1
trace(myTimer.delay);
}
private function myButton2Click(ev:MouseEvent):void {
myTimer.delay = myTimer.delay + 1000;
trace(myTimer.delay);
}
private function runMany(e:TimerEvent):void {
var loader:Loader=new Loader();
var url:String= "http://google.com.example2";
loader.load(new URLRequest(url));
addChild(loader);
}
}

Related

How to stop the timer and move to next scene when puzzle is solved

I created a jigsaw in action script and kept it a separate class. In the project i linked the code and i added a timer to the scene so that the player have to solve the puzzle within the time specified. Now what i want is that if the player solved the puzzle correctly the timer will stop and go to next scene.
this is the puzzle.as
var xpos:Array=new Array();
var ypos:Array=new Array();
var numDone,i:int;
var numPieces:int=3;
var dif:int=25; //how close do they have to drag the piece
var sound:Sound=new Sound();
function playSound(file:String): void {
//var sound:Sound=new Sound();
var req:URLRequest=new URLRequest(file);
sound.load(req);
sound.play();
} //playSound
function scramble(): void {
for(i=0;i<numPieces;i++){
this["piece"+i].x=Math.random()*300;
this["piece"+i].y=Math.random()*500;
this["piece"+i].alpha=.8;
} //for each piece
numDone=0;
txtMessage.text="Drag the pieces to solve the puzzle";
sound.close();
} //scramble
function init(): void {
for(i=0;i<numPieces;i++){
xpos[i]=this["piece"+i].x;
ypos[i]=this["piece"+i].y;
this["piece"+i].addEventListener(MouseEvent.MOUSE_DOWN, beginMove);
this["piece"+i].addEventListener(MouseEvent.MOUSE_UP, endMove);
} //for each piece
txtMessage.text="Drag the pieces to solve the puzzle";
} //init
function beginMove(e:MouseEvent): void {
if(e.target.alpha<1)
e.target.startDrag();
} //beginMove
function endMove(e:MouseEvent): void {
if(e.target.alpha<1) {
e.target.stopDrag();
//figure out which piece it is
var piece:int=0;
while(e.target!=this["piece"+piece]){
piece++;
} //
var curX:int=this["piece"+piece].x;
var curY:int=this["piece"+piece].y;
if(curX<=xpos[piece]+dif && curX>=xpos[piece]-dif &&
curY<=ypos[piece]+dif && curY>=ypos[piece]-dif) { //close enough
this["piece"+piece].x=xpos[piece];
this["piece"+piece].y=ypos[piece];
this["piece"+piece].alpha=1;
numDone++;
if(numDone>=numPieces) gameOver();
else playSound("http://www.zebra0.com/langResource/ok.mp3");
} //in position
} //not already done
}//endMove
function gameOver(): void {
txtMessage.text="You did it!";
playSound("http://www.zebra0.com/langResource/win.mp3");
gotoAndStop(1,"Scene 2");
} //gameOver
function scrambleHandler(e:MouseEvent): void {
scramble();
} //clickHandler
btnScramble.addEventListener(MouseEvent.CLICK,scrambleHandler);
function solve(e:MouseEvent): void {
for(i=0;i<numPieces;i++) {
this["piece"+i].x=xpos[i];
this["piece"+i].y=ypos[i];
this["piece"+i].alpha=1;
} //for each piece
} //solve
the code for linking the puzzle.as to the project. i kept in a layer called 'ActionScript'
include "Puzzle.as"
numPieces=20;
init();
scramble();
stop();
timer code i kept in a layer called 'action'
var nCount:Number = 90;
var myTimer:Timer = new Timer(1000, nCount);
timer_txt.text = nCount.toString();
myTimer.start();
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onComplete);
function countdown(e:TimerEvent):void
{
nCount--;
timer_txt.text = nCount.toString();
}
function onComplete(e: TimerEvent):void{
gotoAndStop(35);
}
if any one have an idea of how to achieve this.

How do I stop my Flash (AS3) program's framerate from dropping when users go to a new tab in their browser?

Every time I run my Flash program in a browser, the framerate drops from 30 to ~2 whenever I go to a different tab. Is there a way to stop the background framerate from tanking whenever the window isn't in focus? The framerate doesn't go down if I open a new window in the browser, even if the window covers the window with the Flash program. I'm also working in FlashDevelop if that helps. Thanks!
Unearthed some source code from 2014 and found what I ended up going with:
HUD.as:
public var idle:Boolean = false;
public var catchingUp:Boolean = false;
private var secondsIdle:int = 0;
stage.addEventListener(Event.DEACTIVATE, StartIdleMode);
stage.addEventListener(Event.ACTIVATE, RegainedFocus);
public function Update(sta:DisplayObjectContainer, save:SharedObject):void
{
//...The game logic that happens every frame...
if (secondsIdle < 0)
{
idle = true;
}
}
private function StartIdleMode(e:Event):void
{
var date:Date = new Date();
secondsIdle = date.valueOf();
}
private function RegainedFocus(e:Event):void
{
if (idle)
{
var diffDate:Date = new Date();
secondsIdle += -(diffDate.valueOf() / 2);
catchingUp = true;
}
}
public function CatchUp(sta:DisplayObjectContainer, save:SharedObject):void
{
var tempFrames:int = Math.floor(secondsIdle / 1000 * 30);
while (tempFrames < 0)
{
this.Update(sta, save);
tempFrames++;
}
idle = false;
catchingUp = false;
secondsIdle = 0;
}
Main.as:
private var gameTimer:Timer = new Timer(1000 / 30, 0);
private var hud:HUD;
public function Main():void
{
gameTimer.addEventListener(TimerEvent.TIMER, GameLoop);
gameTimer.start();
}
private function GameLoop(e:TimerEvent):void
{
if (!hud.idle)
{
hud.Update(stage, gameSystem.saveData);
}
if (hud.catchingUp)
{
hud.CatchUp(stage, gameSystem.saveData);
}
}

Away3d mesh not getting mouseClick events

I am just trying to get the coordinates of the mouse in a plane but the mouse event just doesnt fire.
I also have an starling instance on top of away3d that i got with this tutorial and i believe there is a problem there.
Here is my code:
public class Main extends Sprite
{
private var view3D:View3D;
private var stage3DManager:Stage3DManager;
private var stage3DProxy:Stage3DProxy;
public function Main():void
{
if (stage)
init();
else
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
view3D = new View3D();
initProxies();
}
private function initProxies():void
{
stage3DManager = Stage3DManager.getInstance(stage);
stage3DProxy = stage3DManager.getFreeStage3DProxy();
stage3DProxy.addEventListener(Stage3DEvent.CONTEXT3D_CREATED, onContextCreated);
}
private function onContextCreated(event:Stage3DEvent):void
{
initAway3D();
initStarling();
var floor:Mesh = new Mesh(new PlaneGeometry(600, 400), new ColorMaterial(0x530000));
floor.mouseEnabled = true;
view3D.scene.addChild(floor);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
floor.addEventListener(MouseEvent3D.CLICK, onClick);
}
private function onClick(e:MouseEvent3D):void
{
trace("Click");
}
private function initAway3D():void
{
view3D.stage3DProxy = stage3DProxy;
view3D.shareContext = true;
addChild(view3D);
view3D.mousePicker = PickingType.SHADER;
view3D.camera = new Camera3D(new OrthographicLens());
view3D.camera.x = 1000;
view3D.camera.y = 1000;
view3D.camera.z = 1000;
view3D.camera.lookAt(new Vector3D(0, 0, 0));
}
private function initStarling():void
{
var starling:Starling = new Starling(StarlingSprite, stage, stage3DProxy.viewPort, stage3DProxy.stage3D);
starling.showStats = true;
starling.start();
}
private function onEnterFrame(e:Event):void
{
stage3DProxy.clear();
view3D.render();
stage3DProxy.present();
}
}
}
I'm working on a small project, I based it from this tutorial, and everything works fine!
I had a problem with feathers but I resolved it when I called starling.start() which is not in the tutorial, but you've already did it :)
Try this :
private function initStarling():void
{
starling = new Starling(StarlingSprite, stage, stage3DProxy.viewPort, stage3DProxy.stage3D);
starling.addEventListener(Event.ROOT_CREATED, rootCreatedHandler);
}
private function rootCreatedHandler(event:Event):void
{
starling.removeEventListener(Event.ROOT_CREATED, rootCreatedHandler);
stage3DProxy.addEventListener(flash.events.Event.ENTER_FRAME, onEnterFrame);
starling.start();
}
private function onEnterFrame(event:flash.events.Event):void
{
view3D.render();
starling.nextFrame();
}
I hope that it will help you!

[AS3]How to make text appear for a few seconds

Im having trouble keeping doing this. In my update function i have an if statement.
if (_score == 30)
{
timeIncreaseText();
}
timeIncreaseText function is
private function timeIncreaseText():void
{
var textformat:TextFormat = new TextFormat();
textformat.size = 18;
var mytextfield:TextField = new TextField();
mytextfield.defaultTextFormat = textformat;
addChild(mytextfield);
mytextfield.text = "Time has increased. Better hurry!";
mytextfield.textColor = 0xff0000;
mytextfield.width = 500;
mytextfield.x = 100;
mytextfield.y = 200;
}
This works great but i cant seem to make it go away after a few seconds. Help!
A custom subClass would be a good way to do this. You can extend the regular TextField class to include a timer that removes the text field after the specified time.
package
{
import flash.events.Event;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.Timer;
//extend TextField so you get all it's functionality
public class TimedText extends TextField
{
private var timer:Timer;
//first parameter is the text you want to show, second is how many milliseconds before it disappears, third is a different textFormat if you wanted.
public function TimeText(startingText:String, time:Number = 5000, textFormat_:TextFormat = null):void {
super();
this.text = startingText;
if (!textFormat_) { //if a text format isn't passed in, create one with the default settings
textFormat_ = new TextFormat();
textFormat_.size = 18;
textFormat_.color = 0xff0000;
}
this.defaultTextFormat = textFormat_;
timer = new Timer(time, 1); //create a timer that runs only one time
timer.addEventListener(TimerEvent.TIMER, timerTick, false, 0, true); //listen for the timer event
this.addEventListener(Event.ADDED_TO_STAGE, addedToStage, false, 0, true);
}
//use add to stage so the timer doesn't start until the text field is actually visible
private function addedToStage(e:Event):void {
timer.start();
}
private function timerTick(e:TimerEvent):void {
this.dispatchEvent(new Event(Event.COMPLETE)); //if you want something else to handle the removing
//or animate / fade out first
//or directly remove itself
if (this.parent) {
this.parent.removeChild(this);
}
}
}
}
Then you can just do this:
var mytextfield:TimedText = new TimedText("Time has increased. Better hurry!");
addChild(mytextfield);
mytextfield.width = 500;
mytextfield.x = 100;
mytextfield.y = 200;

Sound recording with timer event

I got this code for recording users sound:
public class Main extends Sprite
{
private var mic:Microphone;
private var waveEncoder:WaveEncoder = new WaveEncoder();
private var recorder:MicRecorder = new MicRecorder(waveEncoder);
private var recBar:RecBar = new RecBar();
private var tween:Tween;
private var fileReference:FileReference = new FileReference();
public function Main():void
{
recButton.stop();
activity.stop();
mic = Microphone.getMicrophone();
mic.setSilenceLevel(0);
mic.gain = 100;
mic.setLoopBack(true);
mic.setUseEchoSuppression(true);
Security.showSettings("2");
addListeners();
}
private function addListeners():void
{
recButton.addEventListener(MouseEvent.MOUSE_UP, startRecording);
recorder.addEventListener(RecordingEvent.RECORDING, recording);
recorder.addEventListener(Event.COMPLETE, recordComplete);
activity.addEventListener(Event.ENTER_FRAME, updateMeter);
}
private function startRecording(e:MouseEvent):void
{
if (mic != null)
{
recorder.record();
e.target.gotoAndStop(2);
recButton.removeEventListener(MouseEvent.MOUSE_UP, startRecording);
recButton.addEventListener(MouseEvent.MOUSE_UP, stopRecording);
addChild(recBar);
tween = new Tween(recBar,"y",Strong.easeOut, -recBar.height,0,1,true);
}
}
private function stopRecording(e:MouseEvent):void
{
recorder.stop();
mic.setLoopBack(false);
e.target.gotoAndStop(1);
recButton.removeEventListener(MouseEvent.MOUSE_UP, stopRecording);
recButton.addEventListener(MouseEvent.MOUSE_UP, startRecording);
tween = new Tween(recBar,"y",Strong.easeOut,0, - recBar.height,1,true);
}
private function updateMeter(e:Event):void
{
activity.gotoAndPlay(100 - mic.activityLevel);
}
private function recording(e:RecordingEvent):void
{
var currentTime:int = Math.floor(e.time / 1000);
recBar.counter.text = String(currentTime);
if (String(currentTime).length == 1)
{
recBar.counter.text = "00:0" + currentTime;
}
else if (String(currentTime).length == 2)
{
recBar.counter.text = "00:" + currentTime;
}
}
private function recordComplete(e:Event):void
{
fileReference.save(recorder.output, "recording.wav");
}
}
I want to replace mouse events with timer event. If lasted time == 5 then start recording and after
10 seconds stop recording. I confused where add my timer code something like this:
var myIntrotime:Timer = new Timer(1000,5);
myIntrotime.addEventListener(TimerEvent.TIMER, startIntroTime);
myIntrotime.start();
var SecondsElapsed:Number = 1;
function startIntroTime(event:TimerEvent):void
{
if (SecondsElapsed==5)
{
//start recording
//start another timer2 and if timer2 finished stop recording
}
SecondsElapsed++;
}
Could someone help me?
You can do better if you employ flash.utils.setTimeout() to make a delayed call. This makes all the dirty work with timers for you.
setTimeout(startIntroTime,5000)
function startIntroTime():void
{
//start recording
setTimeout(stopRecording,10000);
}
The manual on setTimeout()