Start and stop interval using button [as3] - actionscript-3

I tried to make two buttons, one that starts an interval and one that stops it.
This is my code:
s_start.addEventListener(MouseEvent.CLICK, startRepeater);
s_stop.addEventListener(MouseEvent.CLICK, stopRepeater);
function startRepeater(e:MouseEvent) : void {
setInterval(repeater,500);
}
function stopRepeater(e:MouseEvent) : void {
clearInterval(repeater);
}
The start button works perfectly! but the stop button doesn't.
1067: Implicit coercion of a value of type Function to an unrelated type uint.
Thank you for your help in advance.

The clearInterval function accepts an unsigned integer which is an id to the interval you created not a function. Check out this tutorial for more info.
So you might want to try something like this
var intervalId:uint;
s_start.addEventListener(MouseEvent.CLICK, startspam);
function startspam(e:MouseEvent):void {
intervalId = setInterval(spam,500);
}
s_stop.addEventListener(MouseEvent.CLICK, stopspam);
function stopspam(e:MouseEvent):void {
clearInterval(intervalId);
}

Related

Flash AS3 returning a value after Mouseclick

Hi I need help with returning a variable after a mouse click for some reason my code doesn't work
function LeftMercury(e:MouseEvent):Number
{
var ldiameter:Number = 4800;
return ldiameter;
}
trace(LeftMercury());
import flash.events.MouseEvent;
var ldiameter:Number=0;
stage.addEventListener(MouseEvent.CLICK,LeftMercury);
function LeftMercury(e:MouseEvent):void
{
ldiameter=4800;
trace(ldiameter);// OR
trace(e.stageX); // Mouse Position
}
you can be the solution ?
its hard to help when you simply state the problem and no background knowledge, but from a first glance, i see that calling LeftMercury() with no parameters, when the function expects one is bad. instead make the function set e to null when nothing is supplied, so if you call LeftMercury() with no parameters, the compiler just makes e equal to null by default, placing a MouseEvent in their will work too as it will override the default null value you have set:
function LeftMercury(e:MouseEvent=null):Number
{
var ldiameter:Number = 4800;
return ldiameter;
}
trace(LeftMercury());
This code is perfect, but if it still does not work, I can't help you. You'll have to update your question to provide more background and code for us.
it should be a global variable to do that :
so declare it outside of the function
var ldiameter:Number;
function LeftMercury(e:MouseEvent):void
{
ldiameter = 4800;
}
trace(ldiameter);

AS3: Clicking a button via actionscript

This is my very simple code:
g2.addEventListener(MouseEvent.CLICK, buttG2);
function buttG2(event:MouseEvent):void
{
buttonNote="G2";
addWholeNote();
}
It works great when I click the button, but is it possible to fire this function from another function using Actionscript?
In some other function:
function otherFunction() {
buttG2(null);
}
You pass in null since it is never used. You could also give the parameter a default value like this:
function buttG2(event:MouseEvent = null):void
{
buttonNote="G2";
addWholeNote();
}
And then call the function without any parameters as event will then be null as per default:
function otherFunction() {
buttG2();
}
Use a default parameter of null to allow the function call from elsewhere in your code. You won't be able to get any mouseevent data, but that's probably not an issue. You could pass null into your function as you have it, but I find this to be cleaner.
g2.addEventListener(MouseEvent.CLICK, buttG2);
function buttG2(event:MouseEvent = null):void
{
buttonNote="G2";
addWholeNote();
}
call it like any other function, the param is now optional.
buttG2();

How to add event listeners to the numeric stepper's text box?

I have a numeric stepper and I want to add an event listener to its text box:
use namespace mx_internal;
durationStepper.inputField.addEventListener(Event.CHANGE,durationStepperTextInputChanged);
private function durationStepperTextInputChanged(event:Event):void
{
use namespace mx_internal;
trace(durationStepper.inputField.text);
}
However, the event function does not execute! I put a break point there and it does not reach it! What am I missing here? Thanks.
The problem is that the developer has stopped Change event from bubbling up. You can find it if you go to the source file of the NumericStepper. Here are two functions, which prevent you from getting the event.
override protected function createChildren():void
{
super.createChildren();
if (!inputField)
{
inputField = new TextInput();
//some code
//some code
inputField.addEventListener(Event.CHANGE, inputField_changeHandler);
addChild(inputField);
}
}
private function inputField_changeHandler(event:Event):void
{
// Stop the event from bubbling up.
event.stopImmediatePropagation();
var inputValue:Number = Number(inputField.text);
if ((inputValue != value &&
(Math.abs(inputValue - value) >= 0.000001 || isNaN(inputValue))) ||
inputField.text == "")
{
_value = checkValidValue(inputValue);
}
}
As you can see the second function has
event.stopImmediatePropagation();
In this case you have two options: either you should find another way of implementing your logic, or you can copy the source code of the component and eliminate this code line.
It would be fine to override the function, but it is private.
You can read about this common problem here
I have tried to choose the second way. It works perfectly! It is not only the *.as file, but some others, which are used in it.
You can download the component here.

Is this the correct way to remove a timer?

I'm not sure if the way I did makes the garbage collector remove the timer. Here are my two functions:
public function newWave() {
var callTimer:Timer = new Timer(800);
callTimer.start();
leftToSpawn = 4;
callTimer.addEventListener(TimerEvent.TIMER,waveCall);
}
public function waveCall(e:TimerEvent) {
leftToSpawn--;
if(leftToSpawn <= 0){
e.target.stop();
e.target.removeEventListener(TimerEvent.TIMER_COMPLETE,waveCall);
}
spawnEnemy();
}
Thanks
To remove an event listener you need to remove it with the exact same signature.
If you do:
.addEventListener(TimerEvent.TIMER,waveCall);
Then you need to use the same event type and function to remove it:
.removeEventListener(TimerEvent.TIMER,waveCall);
Using TimerEvent.TIMER_COMPLETE here will try to remove a listener that doesn't exist, which is silently ignored.
Using target here is ok, for other listener types you may need to use currentTarget, which is always the object the listener got added to. For example in a mouse click event, target could be a child of a MovieClip and without any listeners.
Moreover, timer already has ability to repeat specified number of times and the correct code will be like this:
public function newWave() {
var callTimer:Timer = new Timer(800, 4); //repeat 4 times
callTimer.start();
callTimer.addEventListener(TimerEvent.TIMER,waveCall);
}
public function waveCall(e:TimerEvent) {
spawnEnemy();
}
just change:
e.target.removeEventListener(TimerEvent.TIMER_COMPLETE,waveCall);
to
e.target.removeEventListener(TimerEvent.TIMER,waveCall);

Passing e:MouseEvent as an argument via setInterval

So i have this function
capture_mc.buttonMode = true;
capture_mc.addEventListener(MouseEvent.CLICK,captureImage);
function captureImage(e:MouseEvent):void {
//lalalala
}
I want to call this function every 2 seconds (after mouse click event happens).
I tried using setInterval
setInterval(captureImage,2000,e:MouseEvent);
but it leads to following error
1084: Syntax error: expecting rightparen before colon.
What's wrong ?
And ya, i am new to AS.
First, since this is AS3 you should be using Timer and TimerEvent. I'll show you how in the example.
Now you'll need to separate your functions:
edit: I've updated this to be safer based on #(Juan Pablo Califano) suggestions. I would keep the same timer for ever if the amount of time isn't going to change.
// first param is milliseconds, second is repeat count (with 0 for infinite)
private var captureTimer:Timer = new Timer(2000, 0);
captureTimer.addEventListener(TimerEvent.TIMER, handleInterval);
function handleClick(event:MouseEvent):void
{
// call here if you want the first capture to happen immediately
captureImage();
// start it
captureTimer.start();
}
function handleInterval(event:TimerEvent):void
{
captureImage();
}
function captureImage():void
{
// lalalala
}
You can also stop the timer with captureTimer.stop() whenever you want.
The problem is that you should use the parameterName:ParameterType syntax only when declaring formal parameters (or when declaring vars and consts). Meaning, this is valid only when you are defining a function:
function func(paramName:Type){
}
When you call the function, you don't have to put the type of the arguments.
So, your function call should look like this:
setInterval(captureImage,2000,e);