how do you access property from one class instance to another, assuming both MCButtonA and MCButtonB instances are defined in the main class. This is without using static var.
I keep on getting:
1120: Access of undefined property varA
package {
import flash.display.MovieClip;
public class MCButtonA extends MovieClip {
public var varA:String = "abc";
public function MCButtonA() {
// constructor code
}
}
}
package {
import flash.display.MovieClip;
public class MCButtonB extends MovieClip {
public var varB:String = "abc";
public function MCButtonB() {
// constructor code
trace( ?????varA)
}
}
}
Main class:
var aButton:MCButtonA = new MCButtonA();
var bButton:MCButtonB = new MCButtonB();
trace(this.parent.aButton.varA);
upd
will work only after both buttons are added to stage
Related
I got an error : access of possibly undefined property destinationY through a reference with static type Gold. I don't use the destinationY property in Gold class. Only in main class.
I'm still amateur at classes and defining properties for a object itself. Please help. Thanks!
Here is the Main class:
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
public class Main extends MovieClip
{
private var field:Array;
public var gold:Gold;
public var goldContainer:Sprite = new Sprite();
private var goldTimer:Timer = new Timer(2000);
public function Main():void
{
setupField();
goldSet();
addEventListener(Event.ENTER_FRAME,onEnterFrm);
}
private function goldSet():void
{ addChild(goldContainer);
goldTimer.start();
goldTimer.addEventListener(TimerEvent.TIMER, newGold);
}
private function newGold(e:TimerEvent):void {
var goldRow:int=Math.floor(Math.random()*5);
var goldCol:int=Math.floor(Math.random()*9);
gold = new Gold(this);
gold.buttonMode=true;
goldContainer.addChild(gold);
gold.x=30+goldCol*65;
gold.destinationY = 35+goldRow*75;
gold.y=-2
}
private function onEnterFrm(e:Event):void {
for (var i:uint=0; i<goldContainer.numChildren; i++) {
var fallingGold:Gold=goldContainer.getChildAt(i) as Gold;
if (fallingGold.y<fallingGold.destinationY) {
fallingGold.y++;
} else {
fallingGold.alpha-=0.01;
if (fallingGold.alpha<0) {
goldContainer.removeChild(fallingGold);
}
}
}
}
}
}
Here is the Gold class :
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Sprite
import flash.events.MouseEvent
public class Gold extends MovieClip
{
private var _main:Main
private var gold:Gold;
public function Gold(main:Main):void
{
_main=main;
addEventListener(MouseEvent.CLICK, goldClicked);
}
private function goldClicked(e:MouseEvent):void
{
e.currentTarget.removeEventListener(MouseEvent.CLICK,goldClicked);
_main.goldContainer.removeChild(e.currentTarget as Gold);
}
}
}
From first glance, you don't have a destinationY property in your Gold Class. MovieClip does not have this property either, so you're trying to manipulate a variable that doesn't exist.
Unless this isn't the whole Class you're showing us, of course.
To elaborate a bit
I don't use the destinationY property in Gold class. Only in main
class.
Yes, but the you're trying to manipulate a property called destinationY in your Gold Class from the main class.
If you do this:
public class Gold extends MovieClip
{
private var _main:Main
private var gold:Gold;
public var destinationY:int = 0;
...
}
you shouldn't get this error anymore, but i doubt this will solve all your problems.
On line xx of the Main class, you have the code:
gold.destinationY = 35+goldRow*75;
You're trying to access the "destinationY" property of the Gold class, but the Gold class doesn't have a public property named destinationY. To add it, just add this line to the Gold class:
public var destinationY;
How do I fix this Error #1010: : A term is undefined and has no properties. at Main/showIntro() at Main().
The code that I used is from a online tutorial.
package {
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.MovieClip;
public class Main extends MovieClip {
private var intro:Introduction;
public function Main() {
intro = new Introduction();
showIntro();
}
private function showIntro():void {
//add intro
addChild(intro);
//add eventlistener
intro.begin_btn.addEventListener(MouseEvent.CLICK, clickBegin);
intro.x = stage.stageWidth/2;
intro.y = stage.stageHeight/2;
}
private function clickBegin(e:MouseEvent):void {
trace("0");
}
}
}
}
I'm pretty sure this is because begin_btn isn't defined in the Introduction class when you instantiate it.
Try adding a definition in the class like so:
public var begin_btn:MovieClip;
If you have defined it, check that it is a publicly accessible
In Main.as I have the following:
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
public var damage:Number;
public function Main() {
// constructor code
var char:Character = new Character();
addChild(char);
}
}
}
And I have another package called Character.as
package {
import flash.display.MovieClip;
public class Character extends MovieClip{
public function Character() {
trace(damage);
}
}
}
I need to be able to share the damage set in the main.as with the character. Is there any way to make the speed more global?
Why don't you make damage a public property of your Character and then it'll be easily accessible via your Main class like this :
char.damage = 100;
trace (char.damage);
To do this, just add the property to your Character class like so :
public class Character extends MovieClip {
public var damage:Number;
public function Character() {
trace(damage);
}
}
But given your comment, I take it you would rather everything just be global and accessible everywhere as opposed to applying OOP concepts.
If so... just define it as a public static in your Main class like this :
public static var damage:Number;
and to access it anywhere you do this :
Main.damage = 100;
trace(Main.damage);
There is another way of sending values through packages (This way is not really sharing variables, but it could be useful for you). What this code does is that the class Character creates a variable, and this variables gets an value from the Main package:
Change the character.as to this:
package {
import flash.display.MovieClip;
public class Character extends MovieClip{
public function Character(a:int) {
//output will be the integer 10
trace(a);
}
}
}
and main.as to:
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
private var damage:int = 10;
private var char:Character = new Character(damage);
public function Main() {
}
}
}
Edit: Not useful for realtime applications, because the values of private var damage will only be send on initialization of private var char:Character = new Character(damage).
I am trying to access vars, functions and objects hard coded on the main timeline from a class. The Objects, vars etc... are loaded when I call a function in the class like this:
Some code from main timeline:
import com.beauMoves;
var bm = new beauMoves();
bm.thisWorks();
and below is the class. But it is not accessing the main timeline. In this case I am trying to access a display object loaded from the lib and place on the timeline. The object is called "Beau" as you can see in the code below.
package com {
import flash.display.MovieClip;
import com.*;
public class beauMoves extends MovieClip
{
public function beauMoves()
{
// constructor code
trace("BeauMoves");
}
public function thisWorks()
{
trace("Cool Beans! This one worked");
// THESE TWO LINES BELOW ARE NOT WORKING
var main:MovieClip = MovieClip(this.parent);
main.Beau.alpha = .3;
}
}
}
Presuming the two lines below are on the main timeline, pass to beauMoves() this as a constructor argument:
import com.beauMoves;
var bm = new beauMoves( this );
Then in your beauMoves() class:
package com {
import flash.display.MovieClip;
import com.*;
public class beauMoves extends MovieClip
{
private var _mainTimeline:MovieClip;
public function beauMoves( mainTimeLine:MovieClip )
{
// constructor code
trace("BeauMoves");
_mainTimeline = mainTimeLine;
}
public function thisWorks()
{
trace("Cool Beans! This one worked");
_mainTimeline.bm.alpha = .3;
}
}
}
I want to acces the textfield I have placed on the stage, with instance name texx from an external as3 file with code.
package src
{
import flash.display.Stage;
import flash.display.MovieClip;
public class Global
{
public static var _stage:Stage;
public static var r:MovieClip = MovieClip(root);
}
public function Global()
{
r.texx.text = "some text"
}
}
}
as you guessed it is not displaying the text i want. I searched high and low but couldn't find on how to access stage objects from external classes that are inside a package. any help would be appreciated
Solved the problem by adding the text field as a class object from inside a movie clip, just created, gave the movie clip (inside which is the text field tex) class name src.texter then in as class
package src
{
import flash.display.Stage;
import flash.display.MovieClip;
import src.texter;
public class Global
{
public var texxx:texter = new texter;
public function Global()
{
addChild(texxx);
texxx.x = 336;
texxx.y = 330;
texxx.tex.text = "some text";
}
}
You can't access stage instance from non display class, Stage hasn't static getter to its instance. The only way is to set the property stage from a view class, for example:
package src
{
import flash.display.Stage;
import flash.display.MovieClip;
public class Global
{
private static var _stage:Stage;
public static function set stage(value:Stage):void
{
if(stage != value)
{
_stage = value;
init();
}
}
public static function get stage():Stage
{
return _stage;
}
private static function init():void
{
stage.texx.text = "some text";
}
}
}
and code in the document class :
Global.stage = stage;
The same if for root.