PDA

View Full Version : reading objects from a class


jahc
08-28-2009, 12:39 PM
I've got a document class for Main.. and I have another document class called MyClass that extends Sprite.. I've created an Array for dynamically creating objects, and I add the "MyClass" objects to this Array .. theres about 4 items in there currently. So I currently have array[0] array[1] array[2] and array[3]..

Anyway, when I want to access array[0].x and array[0].y (for example) from inside array[1] when it receives an event, how do I read the other array[] objects ?? "root." seems to be the root of the currently read object, I cant seem to access the stage from the Main document class...

I'm very new to this, go easy on me guys!

Thanks :)

raydowe
09-03-2009, 11:14 PM
from inside a class, you can only reference objects that are created inside that class, or have been passed in as a parameter.

Your specific situation is a little confusing. If you could you post your code or a link to your project files it would help.

omgnoseat
11-06-2009, 02:16 PM
I have the same problem, I have a movieclip already placed on the stage with an document class called "player" linked to it.

Whenever I try to acces the movieclip I get the follow error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at KeyObject/construct()
at KeyObject()
at player()

Whenever I try the code on the timeline, everything works fine.


package{
import flash.display.MovieClip;
//import flash.display.stage;
import flash.display.Stage;
import flash.events.Event;
import flash.text.TextField;

public class player extends MovieClip {
//public variables

private var stageRef:Stage;

public var Key:KeyObject = new KeyObject(stageRef);

public var xSpeed:Number = 10;
public var ySpeed:Number = 5;

public var dy:Number = 0;

public var gravity:Number = 0.6;

public var canjump:Boolean = false;

public var collectedCDS:Number = 0;

//var stage = flash.Lib.current.stage;


//ENABLE STAGE REFERENCE
// public var character:MovieClip;
//public var block:MovieClip;
//public var cd:MovieClip;
//public var ground:MovieClip;

public function player ():void {


this.stageRef = stageRef;
Key = new KeyObject(stageRef);

//event listeners


stage.addEventListener(Event.ENTER_FRAME,moveChar) ;




function moveChar(e:Event):void{

if(Key.isDown(Key.LEFT)){


character.x-=xSpeed;
character.gotoAndStop(2);
character.scaleX = -1;


} else if(Key.isDown(Key.RIGHT)){


character.x += xSpeed;
character.scaleX = 1;
character.x + character.width/2;
character.gotoAndStop(2);


}else if(Key.isDown(Key.DOWN)){

character.gotoAndStop(1);

}else{
character.gotoAndStop(1);


}


Any idea's?

raydowe
11-06-2009, 05:49 PM
Reading the error message, I can tell that the error is occuring in the function "construct" (maybe the constructor if you don't have a function named that?) in the KeyObject class. Something in that function is null when it shouldn't be.

From what I can see of your code, it's occuring at this line:

public var Key:KeyObject = new KeyObject(stageRef);


What is stageRef? It looks to me like it hasn't been declared which means it's null. I imagine your trying to use it inside the KeyObject constructor, (because your passing it in). This would cause the error your getting.

omgnoseat
11-07-2009, 11:55 AM
Reading the error message, I can tell that the error is occuring in the function "construct" (maybe the constructor if you don't have a function named that?) in the KeyObject class. Something in that function is null when it shouldn't be.

From what I can see of your code, it's occuring at this line:

public var Key:KeyObject = new KeyObject(stageRef);


What is stageRef? It looks to me like it hasn't been declared which means it's null. I imagine your trying to use it inside the KeyObject constructor, (because your passing it in). This would cause the error your getting.
Oh right I should have mentioned that, the keyobject.as is a file from senocular which enables the KEY.isdown function from actionscript 3. I didn't change any code in there so it should be working actually.

The problem is that the character movieclip just isnt recognized for some reason.

raydowe
11-07-2009, 04:04 PM
A little confused by your last post, but looking more closely I see the problem.


private var stageRef:Stage;
public var Key:KeyObject = new KeyObject(stageRef);

Your passing in a variable to the KeyObject constructor that hasn't been instantiated. It's defined as type "Stage", but is still null.

I remember having problems with stage references before. Try passing it in to the constructor, and don't instantiate the KeyObject until then.


public class player extends MovieClip
{
public var Key:KeyObject;
public var xSpeed:Number = 10;
public var ySpeed:Number = 5;
public var dy:Number = 0;
public var gravity:Number = 0.6;
public var canjump:Boolean = false;
public var collectedCDS:Number = 0;

public function player (stageRef):void {
this.stageRef = stageRef;
Key = new KeyObject(stageRef);
}



then use

var p:player = new player(this.stage);

fantarie
11-15-2009, 03:55 PM
A little confused by your last post, but looking more closely I see the problem.


private var stageRef:Stage;
public var Key:KeyObject = new KeyObject(stageRef);

Your passing in a variable to the KeyObject constructor that hasn't been instantiated. It's defined as type "Stage", but is still null.

I remember having problems with stage references before. Try passing it in to the constructor, and don't instantiate the KeyObject until then.


public class player extends MovieClip
{
public var Key:KeyObject;
public var xSpeed:Number = 10;
public var ySpeed:Number = 5;
public var dy:Number = 0;
public var gravity:Number = 0.6;
public var canjump:Boolean = false;
public var collectedCDS:Number = 0;

public function player (stageRef):void {
this.stageRef = stageRef;
Key = new KeyObject(stageRef);
}



then use

var p:player = new player(this.stage);

I thinks this code is very good!

__________________
Simulateur de pret immobilier simulation credit | (http://simulateurdepret.org/)Simulateur pret immobilier simulation de credit | (http://simulateurdepret.org/)Simulateur de pret immo (http://simulateurdepret.org/simulateur-de-pret/)