Action Script 3 .Loop the animation 3 times and stop - actionscript-3

I working on Flash banners and I would like the animation to loop 3 times and then stop. I don't know Action Script but I found this code:
On the 1st frame I used this:
_root.loops_played = 0;
if (_root.loops_played >= 3){
stop(); //*
} else {
play();
}
On the last frame:
_root.loops_played += 1;
Unfortunately the animation keeps looping and I get this error:
Scene 1, Layer 'actions', Frame 1, Line 1 1120: Access of undefined property _root.
Scene 1, Layer 'actions', Frame 1, Line 3 1120: Access of undefined property _root.
Scene 1, Layer 'actions', Frame 175, Line 1 1120: Access of undefined property _root.
Scene 1, Layer 'actions', Frame 175, Line 1 1120: Access of undefined property _root.
Could anyone help please?

_root doesn't exist in AS3. It is an AS2 thing.
Still, to get your current logic running, use this instead of _root.
Here is some code with not much changes to what you provided:
First Frame:
if(!this.loops_played) this.loops_played = 1;
if(this.loops_played >= 3) stop(); else play();
Last Frame:
this.loops_played++;

Put this code into the last frame or where you want to stop your movie after playing 3 times.
if(!loopCount){var loopCount:Number = 0;}
loopCount++;
if(loopCount >= 3){stop();}

The compiler explicitly tells you that the _root property is undefined. You can not use undefined properties and objects.
_root does not exist in AS3 any more, there is a slightly comparable property root. Keep in mind that root is not global.

Related

Drag & Drop - ActionScript3

I am developing a simple flash game - a drag and drop game for children.
I have created three animal instances which will be dragged and dropped to the correct space. I am just adding action script and am getting the following error when trying to add drag and drop functions;
Scene 1, Layer 'Actions', Frame 1, Line 10, Column 44 1119: Access of possibly undefined property startDragging through a reference with static type String.Scene 1, Layer 'Actions', Frame 1, Line 10, Column 44 1136: Incorrect number of arguments. Expected 2.
My code is as follows;
import flash.events.MouseEvent;
stop();
//set up the buttons for the puzzle pieces
Pig.buttonMode=true;
Pig.addEventListener(MouseEvent.MOUSE_DOWN.startDragging);
Pig.addEventListener(MouseEvent.MOUSE_UP.stopDragging);
function startDragging(e:MouseEvent){
trace("startDragging");
}
function stopDragging(e:MouseEvent){
trace("stopDragging");
}
Should be
Pig.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
Pig.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
MouseEvent.MOUSE_UP is a String and it doesn't have a property called startDragging. To add a listener you should pass a function reference as a second argument of addEventListener (using comma, no a dot)

How to create bmi calculator inside flash cs5.5 using actionscript 3.0?

I'm not very familiar with flash so this is my current problem to deal with. I've found some code but it's in actionscript 2.0, when i tried to run it in my project, it shows the following error.
Here are my errors:
1. Scene 1, Layer 'Layer 2', Frame 1, Line 6
1067: Implicit coercion of a value of type Number to an unrelated type flash.text:TextField.
Scene 1, Layer 'Layer 2', Frame 1, Line 8
1180: Call to a possibly undefined method on.
Scene 1, Layer 'Layer 2', Frame 1, Line 8
1120: Access of undefined property release.
var weight_BMI;
var height_BMI;
var BMI_FINAL;
total_BMI=Number(weight_BMI.text)/(Number(height_BMI.text)*Number(height_BMI.text));
on(release){
trace(weight_BMI.text)
trace(height_BMI.text)
trace(BMI_FINAL)
}
AS2 is very different from AS3. AS3 works by using methods (in the same way as Java, for example). See the code below as an example (note, you may need to edit/rename your fields to get it to compile)
It works by attaching a Click EventListener to your calculate button which when triggered it runs the calculateBMI method. This method then performs the calculation and prints the result to your text field.
var myBmi:TextField;
var total_BMI:Number;
function calculateBMI(e:MouseEvent):void
{
total_BMI = Number(weight_BMI.text)/(Number(height_BMI.text)*Number(height_BMI.text));
myBmi.text = String(total_BMI);
}
btnCalculate.addEventListener(MouseEvent.CLICK, calculateBMI);

Error to hitTestObject with children objects (system warning that the property and the method are not defined)

I'm trying to check if there's any children object being collided on "player" object.
for (var ii:uint = 0; ii < opponentNave.numChildren; ++ii) {
if (player.hitTestObject(opponentNave.getChildAt(ii))) {
trace("...");
}
}
I got this method from hitTestObject on child of a MC, I'm trying to use it, but I given this error (my Flash Professional Cs6 is on Spanish language, then I translated with Google Tradutor):
Scene 1, Layer 'functions', Frame 1, Line 57 1061: Call to a possibly undefined method getChildAt through a reference with static type Class.
----------------------
Scene 1, Layer 'functions', Frame 1, Line 56 1119: Access to undefined property numChildren possibly through a reference with static type Class.
Could something be missing (like a import)?

I'm trying to create a sliding gallery in AS3 in Flash CS5, but I get these errors: does it have something to do with AS3 ?

Scene 3, Layer 'AS', Frame 1, Line 1 1119: Access of possibly
undefined property onEnterFrame through a reference with static type
flash.display:DisplayObject. Scene 3, Layer 'AS', Frame 1, Line 2
1119: Access of possibly undefined property _ymouse through a
reference with static type flash.display:DisplayObject. Scene 3, Layer
'AS', Frame 1, Line 3 1120: Access of undefined property myVar.
Scene 3, Layer 'AS', Frame 1, Line 5 1119: Access of possibly
undefined property _ymouse through a reference with static type
flash.display:DisplayObject. Scene 3, Layer 'AS', Frame 1, Line 6
1120: Access of undefined property myVar. Scene 3, Layer 'AS', Frame
1, Line 8 1119: Access of possibly undefined property _xmouse
through a reference with static type flash.display:DisplayObject.
Scene 3, Layer 'AS', Frame 1, Line 8 1120: Access of undefined
property myVar. Scene 3, Layer 'AS', Frame 1, Line 17 1119: Access
of possibly undefined property _xmouse through a reference with static
type flash.display:DisplayObject. Scene 3, Layer 'AS', Frame 1, Line
17 1120: Access of undefined property myVar. Scene 3, Layer 'AS',
Frame 1, Line 24 1119: Access of possibly undefined property _xmouse
through a reference with static type flash.display:DisplayObject.
Scene 3, Layer 'AS', Frame 1, Line 24 1119: Access of possibly
undefined property _xmouse through a reference with static type
flash.display:DisplayObject. Scene 3, Layer 'AS', Frame 1, Line 24
1119: Access of possibly undefined property _xmouse through a
reference with static type flash.display:DisplayObject.**
the code is:
enter code here
root.onEnterFrame = function() {
if(root._ymouse<601){
myVar=false;
}
if(root._ymouse>600){
myVar=true;
}
if(root._xmouse<100 && myVar==true)
{
imgBar.prevFrame();
imgBar.prevFrame();
imgBar.prevFrame();
}
else{
imgBar.play;
}
if(root._xmouse>600 && myVar==true){
imgBar.nextFrame();
imgBar.nextFrame();
}
else{
imgBar.play;
}
if(root._xmouse>100 && root._xmouse<600 && myVar==true){
imgBar.stop();
}
}
Your code is in AS2. in AS3, objects use the addEventListener property instead of constructs like onEnterFrame.
Change the targeting of your project to AS2, or you can just revise the top line of code like so:
root.addEventListener(Event.ENTER_FRAME, function() {
... // your code
})

flash as3 timeline animation storing value

I set a value on frame 20 of my animation
var final_answer = hundreds.text+tens.text+units.text;
var mySharedObject:SharedObject = SharedObject.getLocal("/");
I want to access this on frame 40 but I get the following error
Scene 1, Layer 'Actions', Frame 210, Line 5 1120: Access of undefined property mySharedObject.
I have also tried to use a normal variable - is there anyway to store a variable in what is effectively a cookie between frames?
I suggest you to use document class but if you don't want and handle all in the first frame you can use addFrameScript
addFrameScript(4,onFrame5);
function onFrame5():void{
trace('current frame is 5');
}