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