Changing value of global variable from inside a movieclip - actionscript-3

I tried to make a movieclip, then I inserted a script in the movieclip.
so there are two variables, Score as the dynamic text is maintained, and there is a point as the dynamic text inside the movieclip. I want the point value to be moved to the Score when the movie clip is complete. or there might be a way to change the contents of the variable outside the movie clip when the clip movie is executed
var point:int = 0;
poin.text = ''+point;
this is the code in my movie clip
so how to pass that "point" as dynamic text to "Score" as dynamic text outside the movieclip(on the main timeline)

You can use static variables and without creating an instance of the class, so they can be used as global variables.
Example:
package
{
class MyClass
{
// ...
public static var myStaticVar: Number;
// ...
}
}
Then anywhere in your program, you can access the variable myStaticVar this way:
MyClass.myStaticVar = 5;
// ...
var value:Number = MyClass.myStaticVar;
Hope it helps.

Related

How to remove and add a Sprite type of data to the scene?

I have a Sprite type of variable:
var can: Sprite;
can= new Sprite();
addChild(can);
can.x = 5;
can.y = 5;
This Sprite is making a grid in my scene as shows below:
How it makes the grid?
I will pass it to a class named "Grid" to make the whole grid.
When I am going to go to another scene, I need to remove all child in current scene. So, I use the function:
function clearAllTut(): void {
//trace(numChildren);
while (numChildren > 0) {
removeChildAt(0);
}
}
when I will come back to this scene again, all the other child are demonstrated except "can".
The problem is with the removing function? (then why all the other child will be demonstrated again except "can")
or I need to change the method of programming for a Sprite type of variable?

AS3: How do I access a variable from a parent object?

I'm new to AS3 and my code may look a bit off.
Basically, how my program works is that I have an instance of "Level" on the stage, and it is a MovieClip containing several other objects which are also MovieClips with their own document class. In the "Level" class, I can access the X and Y position from the instance of "Player", but in my "Arrow" class, which is also a child of Level, I'm unable to access the X and Y of "Player". What I tried to do is set a public static variable called playerX and playerY in the Level class and assign that to the player's x and y position every frame, and I try accessing the variable in the Arrow class by doing "var x:Number = Object(parent).playerX, I've also tried MovieClip(parent).playerX and parent.playerX and just player X, neither of them work.
So to sum it up, I need to access a variable from a parent class, but every way I have tried it just returns an error.
Sorry if this was a bit unclear, any help will be much appreciated!
Sounds like you are using FlashPro Timelines, and have two siblings objects (they have the same parent MovieClip/timeline) and you want to be able to reference one from the other (let me know if this doesn't properly summarize your question).
Since it sounds like you have class files attached to your Arrow and Player classes, you'll want to make sure any code referencing the parent or stage or root vars is run AFTER the item has been added to the display list as those vars are only populated after the item has been put on the stage. (this isn't an issue for timeline code).
So, something like this:
public class Arrow extends MovieClip {
public var player:Player; //a var to hold a local reference to the player
public function Arrow():void {
//right now parent/stage/root are null
//listen for the added to stage event, so you know when it's safe to use the parent and stage and root keywords
this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}
private function addedToStageHandler(e:Event):void {
player = MovieClip(parent).playerInstanceName;
//where player instance name is the instance name of your player on the parent timeline
}
private function doSomethingWithPlayer():void {
if(player){
player.x = 5;
}
}
}
Now, if you wanted to use a Static var instead (might be less cumbersome than the parent keyword), and there is only ever 1 player instance, you could do the following:
public class Player extends MovieClip {
//create a static var to hold a reference to the player instance
public static var me:Player;
public function Player():void {
me = this; //now assign the instance to the static var.
}
}
Now, you can access the player instance from anywhere in your app by doing Player.me (regardless of context)
Player.me.x = 5;
You need to understand what a static variable is and what it is not. A static variable IS a Class variable, A static variable IS NOT an instance variable. So if I create a static variable called "playerX" in a class called "Level" I can now do this:
Level.playerX = 0;
Because Level is a class and playerX is a variable of that class.
I cannot do this:
Object(parent).playerX
Because 'parent' is not a class and does not have a variable called 'playerX', parent might be an instance of the class 'Level' but instances of the class 'Level' do not have a variable called 'playerX', the class itself "Level" is the one that has that variable.

Making a simple tamagoci game getting no compiler errors but receiving no output

Kind of new Actionscript and I'm just trying to make a simple tamagoci game. I've wrote all the code out but and receiving no compiler errors but for some reason I'm also not receiving any output messages for my mouse event listeners. Here is all my code, I really can't find the problem and any help would be greatly appreciated. Thanks.
package{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Main extends MovieClip{
public var feedButton:MovieClip;
public var tamagoci:MovieClip;
public var disButton:MovieClip;
public var dietButton:MovieClip;
public function Main() {
this.init();
}
private function init():void {
this.feedButton.addEventListener(MouseEvent.MOUSE_DOWN, onfeedMouseDownHandler);
this.disButton.addEventListener(MouseEvent.MOUSE_DOWN, ondisMouseDownHandler);
this.dietButton.addEventListener(MouseEvent.MOUSE_DOWN, ondietMouseDownHandler);
}
private function onfeedMouseDownHandler(event:MouseEvent)void{
this.tamagoci.scaleX += 0.1;
this.tamagoci.scaleY += 0.1;
}
private function ondisMouseDownHandler(event:MouseEvent)void{
this.tamagoci.gotoAndPlay(5);
}
private function ondietMouseDownHandler(event:MouseEvent)void{
this.tamagoci.scaleX -= 0.1;
this.tamagoci.scaleY -= 0.1;
}
Are you using Flash Professional?
You're declaring your variable types in your class here;
public var feedButton:MovieClip;
public var tamagoci:MovieClip;
public var disButton:MovieClip;
public var dietButton:MovieClip;
But then in your constructor, all you are doing is running init();
public function Main() {
this.init();
}
So, this could one of a few things. The most likely is that you have declared your variables, but you haven't initialised them. You've created the variables to hold your objects, but according to your code, they're empty. More specifically, a variable or class property that doesn't assign an object to a variable of an object type will contain a default value of null.
You could prove this in your code by simply putting a condition inside your init(); method;
if(tamagoci == null){
trace("I haven't been assigned an object of type class yet!")
}
So it could be 1 of these 3 things:
1: If you have written your own classes for these class properties/variables, then you need to instantiate them with the new keyword. The general syntax is;
variable_name = new ClassName(parameter_1, parameter_2);
If you are using classes you have written yourself, you have to create an instance of the object, assign it to a variable, and then add it to the stage using addChild();. For example, lets say you've written your own Tamagoci class;
tamagoci = new Tamagoci();
tamagoci.x = 100; // set the x location
tamagoci.y = 200; // set the y location
addChild(tamagoci);
Notice the use of Tamagoci. This is just an example, but this is the class name, which shouldn't be confused with variable/property name. It could just have easily been;
tamagoci = new MovieClip();
But then, this is just an empty MovieClip. It needs a property to display on the screen. A Shape, A Bitmap, or another container class object like MovieClip or Sprite (container classes allow you to nest display objects inside them). But on a basic level, it must contain a visual component to appear on the stage.
2:
Have you made Main your document class? This is the class which will get automatically called when your Flash movie plays. To set this, click on your stage, and in the properties dialogue box on the right, under PUBLISH, type in the name of your class, which is "Main".
3:
If you have created MovieClips in your library in Flash Professional, then you need to go to your library, right click the MovieClips, and select properties. From there, you need to make sure Export for Actionscript is ticked.
Now, if you click on your MovieClips on the stage, then open the Properties tab in the top right of Flash Professional's default layout, then right at the top should be a text field, and if you hover over it, Instance name will pop up as a tool tip. This is where you name your stage objects. Once that is done, you have access to them in your timeline.
If this is how you've done this, then you don't need to declare the variables in your main class, as they are already declared on your stage by Flash Professional and instantiated automatically.

Adding dynamic variables to movieClip instance in Flash professional

I have a number of instances of a movieClip on the stage in Flash. I would like to be able to add a dynamic variable to each. For example, I would like to number each instance.
I have tried giving each instance an instance name (eg. box1, box2) and writing the following code in the layer 1 > frame 1 code window
box1.number = 1;
box2.number = 2; etc.
or
box1["number"] = 1;
box2["number"] = 2;
but the variables are undefined when trying to access them in Flash builder.
You should create a custom class for all your movieClips to extend (use as a base class). If you don't know how to do this, create the following MyCustomClassName.as file in the root of your .fla directory.
package {
public class MyCustomClassName extends Sprite { //use MovieClip is your box makes use of the timeline
public var myNumber:int = 0;
public function MyCustomClassName(num:int = 0) {
myNumber = num;
}
}
}
Then on your box object, right click it in the library and bring up the properties/linkage. Set the base class to the path to your custom class .as file
You can leave everything the same and now your boxes will inherit all properties and functions in that base class.
myBoxInstance.myNumber = 5;
OR if instantiating through code:
var box:MyCustomClassName = new MyCustomClassName(5); //creates a new box giving it the number 5

AS3 - Trying to declare the value of a Movie Clip

Okay, I have the following code in my Bullet.as file:
public var impact:MovieClip;
public function Bullet():void
{
addEventListener(Event.ADDED_TO_STAGE, whenAdded);
}
function whenAdded(e:Event)
{
if(this is zArrow){
power = -1;
speed = 15;
impact = arrowImpact;
trace(impact);
}
if(this is Dice){
power = -Math.round(Math.random()*5 + 1);
speed = 10;
impact = diceImpact
}
}
See, I am trying to set the value of "public var impact:MovieClip" as the movie clip "arrowImpact" or "diceImpact". What I want is whenever a bullet collides with an enemy, it leaves an impact image behind and I'm trying to change what impact is shown depending on what bullet is colliding.
I am able to change all of the other variables like power and speed using this setup, but I can't declare which impact movie clip the "impact" movie clip variable is.
From the way I understand your question now, you want to pull these specific movie clips from the Library. If I am not mistaken. To do this, you need to pair each of the movie clips in the library to an AS class that extends MovieClip.
Make sure you check "Export for Actionscript" and create the class you want for each. Then, in your code for the Bullet, you can create a new instance of them. So have it say:
impact = new ArrowImpact)();
or DiceImpact depending on your classes.
Hope this is along the lines of what you wanted.
In order to use these, I would recommend creating a getImpact method along the lines of:
public function getImpactMC():MovieClip
{
return impact;
}
Then all you need to do in your main Document is addChild the proper impact from this method. However, be aware that you need to adjust the x and y values of the impactMC before adding it as a child on the stage to make sure that it displays in the proper position.
Glad this helps!