Home Tutorials Forums Articles Blogs Movies Library Employment Press Buy templates

Go Back   ActionScript.org Forums > ActionScript Forums Group > Simple Stuff (Newbies)

Reply
 
Thread Tools Rate Thread Display Modes
Old 08-28-2009, 12:39 PM   #1
jahc
Registered User
 
Join Date: Aug 2009
Posts: 11
Default reading objects from a class

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

Last edited by jahc; 08-28-2009 at 01:10 PM.. Reason: wrong description
jahc is offline   Reply With Quote
Old 09-03-2009, 11:14 PM   #2
raydowe
Senior Member
 
Join Date: Jan 2007
Posts: 274
Default

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.
raydowe is offline   Reply With Quote
Old 11-06-2009, 02:16 PM   #3
omgnoseat
Registered User
 
Join Date: Nov 2009
Posts: 8
Default

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.

ActionScript Code:
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?
omgnoseat is offline   Reply With Quote
Old 11-06-2009, 05:49 PM   #4
raydowe
Senior Member
 
Join Date: Jan 2007
Posts: 274
Default

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:
ActionScript Code:
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.
raydowe is offline   Reply With Quote
Old 11-07-2009, 11:55 AM   #5
omgnoseat
Registered User
 
Join Date: Nov 2009
Posts: 8
Default

Quote:
Originally Posted by raydowe View Post
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:
ActionScript Code:
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.
omgnoseat is offline   Reply With Quote
Old 11-07-2009, 04:04 PM   #6
raydowe
Senior Member
 
Join Date: Jan 2007
Posts: 274
Default

A little confused by your last post, but looking more closely I see the problem.

ActionScript Code:
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.

ActionScript Code:
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
ActionScript Code:
var p:player = new player(this.stage);
raydowe is offline   Reply With Quote
Old 11-15-2009, 03:55 PM   #7
fantarie
Registered User
 
Join Date: Nov 2009
Posts: 1
Default

Quote:
Originally Posted by raydowe View Post
A little confused by your last post, but looking more closely I see the problem.

ActionScript Code:
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.

ActionScript Code:
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
ActionScript Code:
var p:player = new player(this.stage);
I thinks this code is very good!

__________________
Simulateur de pret immobilier simulation credit | Simulateur pret immobilier simulation de credit | Simulateur de pret immo
fantarie is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT. The time now is 02:16 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2009 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.