Jump to content
  • Advertisement
Sign in to follow this  
tashaklikedi

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

This topic is 1851 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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;

	}

 

 

 

 

:

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!