Jump to content

  • Log In with Google      Sign In   
  • Create Account


ActionScript 3 - calling method from a class which has a child class


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 tashaklikedi   Members   -  Reputation: 218

Like
0Likes
Like

Posted 27 May 2013 - 09:43 AM

Hello all, I need help with some calling issues.

I am trying to create a game engine for my future games in actionscript 3, and I am working on a method called Renderer, which I plan to add as member to all my classes which will be displayed on the screen. I use blitting method. I have a GameLoop method, which is in the Game class, and an Entity class, which incorporates an instance of the Renderer class.

 

I'll try to give examples with code:

 

** EDITED CODE **


//Game.as:

package
{
	//import things
	
	public class Game extends Sprite
	{
		var 	mDisplay:Bitmap;
		var	mDisplayBD:BitmapData;
		var	mEntity:Entity;

		public function Game()
		{
			//initialize things
			mDisplayBD = new BitmapData(Constants.SCREEN_WIDTH,Constants.SCREEN_HEIGHT,false,0x000000);
			mDisplay = new Bitmap(mDisplayBD);
			
			mEntity = new Entity(mDisplayBD);		

			this.addEventListener(Event.ENTER_FRAME,GameLoop);
			this.addChild(mDisplay);
		}
		
		public function GameLoop(e:Event)
		{
			mEntity.Render();
		}
	}

}
//Renderer.as



package 
{
	//import stuff

	public class Renderer
	{
		var mBitmapData:BitmapData;
		var mPoint:Point ;
		var mRect:Rectangle ;
		var mDisplayBD:BitmapData;
		public function Renderer(bitmapdata)
		{
			mBitmapData = bitmapdata;
			this.mDisplayBD = bitmapdata;
			mPoint = new Point();
			mRect = new Rectangle();
			mPoint.x = 0;
			mPoint.y = 0;
		}
		
		public function LoadBitmap(URL:String)
		{
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, OnComplete);
			loader.load(new URLRequest(URL));
		
		}
		
		private function OnComplete(event:Event)
		{
			mBitmapData = event.target.content.bitmapData;
		}
		
		public function Render(posX,posY,clipX,clipY,width,height)
		{
			mDisplayBD.lock();
			mPoint.x = posX;
			mPoint.y = posY;
			mRect.x = clipX;
			mRect.y = clipY;
			mRect.width = width;
			mRect.height = height;
			mDisplayBD.copyPixels(mBitmapData,mRect,mPoint);
			mDisplayBD.unlock();
		}
		
	}
}


//Entitiy.as

package
{
	//import
	
	public class Entity extends Sprite 
	{
		
		var mPosX;
		var mPosY;
		var renderer:Renderer;
		var input:InputManager;
		var mDisplayBD:BitmapData;
		public function Entity (bitmapdata:BitmapData)
		{
			mDisplayBD = bitmapdata
			renderer = new Renderer(mDisplayBD);
			renderer.LoadBitmap("filename.png");
		}
		
		public function Render()
		{
			renderer.Render(mPosX,mPosY,0,0,Constants.SCREEN_WIDTH,Constants.SCREEN_HEIGHT);
		}

	
	}
} 

 

When I create a new renderer instance in the class game, and do something like renderer.Render() in the GameLoop method, it works, but not when I do it as entity.renderer.Render();

 

I have to solve this until tomorrow, please can anyone help me? I searched a lot on the internet, but found nothing... (maybe I dont know what to look for).

 

Edit: dont mind the var input:InputManager; line, I forgot to remove it from the sample code


Edited by tashaklikedi, 27 May 2013 - 11:59 AM.


Sponsor:

#2 BloodyEpi   Members   -  Reputation: 622

Like
0Likes
Like

Posted 27 May 2013 - 11:49 AM

Whats the error?

 

I don't see a call to Entity::BAUBackground(), so Entity::renderer wont get initialized, if that's not the problem, try a debugger and check if everything is initialized.



#3 tashaklikedi   Members   -  Reputation: 218

Like
0Likes
Like

Posted 27 May 2013 - 11:55 AM

Whats the error?

 

I don't see a call to Entity::BAUBackground(), so Entity::renderer wont get initialized, if that's not the problem, try a debugger and check if everything is initialized.

oh damn I'll edit this post because obviously I made a lot of errors trying to make it more understandable. The actual name of the Entity class is BAUBackground actually, but for understandability reasons I changed it to Entity. Let me edit smile.png

 

There is no error it just does not render :)


Edited by tashaklikedi, 27 May 2013 - 11:58 AM.


#4 zqf   Members   -  Reputation: 374

Like
0Likes
Like

Posted 28 May 2013 - 10:22 PM

I dunno if this is helpful, and maybe this is covered by code you've removed to make your example smaller but an observation:
Your renderer is defaulting to using the screen canvas as a source...

public function Renderer(bitmapdata)
{
mBitmapData = bitmapdata;
this.mDisplayBD = bitmapdata;

...If your Loader doesn't complete and replace the source then it will be copying the canvas onto itself and there's no load failure listener so if it fails you won't know about it.

#5 Tchom   Members   -  Reputation: 193

Like
0Likes
Like

Posted 29 May 2013 - 09:26 PM

I think class variables in AS3 are internal  by default. 

 

have you tried:

 

public class Entity extends Sprite {
		protected var mPosX:Number;
		protected var mPosY:Number;
		public var renderer:Renderer;

or use a getter function

public class Entity extends Sprite {
		protected var mPosX:Number;
		protected var mPosY:Number;
		protected  var mRenderer:Renderer;

	public function Entity():void{
		...
	}

	public function get renderer():Renderer{
		return mRenderer;

	}

 

 

 

 

:



#6 tashaklikedi   Members   -  Reputation: 218

Like
0Likes
Like

Posted 30 May 2013 - 06:43 PM

thanks for the replies!

Tchom: it says that the mDisplayBD member of the Render class is null 



#7 Tchom   Members   -  Reputation: 193

Like
0Likes
Like

Posted 30 May 2013 - 10:15 PM

Is it null when you pass it into the renderer? Or just after you call Render()?

 

It may be you're trying to render before the bitmap is finished loading. Try:

 

package 
{
	//import stuff

	public class Renderer
	{
		var mBitmapData:BitmapData;
		var mPoint:Point ;
		var mRect:Rectangle ;
		var mDisplayBD:BitmapData;
		public function Renderer(bitmapdata)
		{
			mBitmapData = bitmapdata;
			this.mDisplayBD = bitmapdata;
			mPoint = new Point();
			mRect = new Rectangle();
			mPoint.x = 0;
			mPoint.y = 0;
		}
		
		public function LoadBitmap(URL:String)
		{
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, OnComplete);
			loader.load(new URLRequest(URL));
		
		}
		
		private function OnComplete(event:Event)
		{
			mBitmapData = event.target.content.bitmapData;
		}
		
		public function Render(posX,posY,clipX,clipY,width,height)
		{
			if(mBitmapData != null){
				mDisplayBD.lock();
				mPoint.x = posX;
				mPoint.y = posY;
				mRect.x = clipX;
				mRect.y = clipY;
				mRect.width = width;
				mRect.height = height;
				mDisplayBD.copyPixels(mBitmapData,mRect,mPoint);
				mDisplayBD.unlock();
			}
		}
		
	}
}


Edited by Tchom, 30 May 2013 - 10:58 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS