Recent posts by ForAltair on Kongregate

Flag Post

Topic: Game Programming / Better solution

But what is native code? As in the code which is generated on compilation? Assembly language? I can use assembly atleast for small operations so that would be a bonus.
But I just need to know how to employ stage3d, doesnt matter if it is available from fp11.

Also you didnt mention if that blitting is good or not or is there other way.

 
Flag Post

Topic: Game Programming / Better solution

So I just spent past few hours researching better ways to boost performance. I read an article on bitmap blitting. On searching I came across this class:-

package
{
	import de.polygonal.ds.Array2;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.getTimer;

	/**
	 * Bitmap-blitter. The source must be a bitmap or sprite with 8 rows representing 8 directions.
	 * Each row contains an animation cycle where column 0 is for idle state.
	 * 
	 * @author Anggie Bratadinata
	 */
	public class SpriteSheet extends Sprite
	{
		//bitmap frames
		protected var _bitmapArray:Array2;
		//sheet source
		protected var _source:DisplayObject;
		//frame size
		protected var _frameWidth:Number;
		protected var _frameHeight:Number;
		//the rendered bitmap
		protected var _frameBitmap:Bitmap;
		protected var _frameRect:Rectangle;

		//current facing direction
		protected var _direction:int;
		//current frame row
		public var _currentRow:Array;
		public var _currentColNum:Number;
		//render timer
		protected var _oldTime:Number = 0;
		protected var _renderInterval:Number;
		
		//is the south-facing sequence on the first row (true) or not (false)?
		protected var _southFirst:Boolean;
		
		//the visual state 
		protected var _isIdle:Boolean;
		
		//if true, a new bitmap will be generated
		protected var _isDirty:Boolean;
		
		var elapsed:Number;

		public function SpriteSheet()
		{

		}

		/**
		 * Build the sprite sheet. The source must contain 8 rows of animation frames arranged in clockwise order.
		 * The first row can be the south face or the south west. A tool like SpriteForge renders tilesheets 
		 * with the south-facing sequence on the first row. 
		 * 
		 * Acceptable row arrangements : S,SW,W,NW,N,NE,E,SE or SW,W,NW,N,NE,E,SE,S
		 * 
		 * @param	source			The source Display object. 
		 * @param	frameWidth		animation frame width
		 * @param	frameHeight		animation frame height
		 * @param	renderInterval	delay between renders. This defines the animation speed in milliseconds
		 * @param	southFirst		the south-facing sequence is on the first row (true) or the last (false). 
		 *                          
		 */
		public function build(source:DisplayObject, frameWidth:Number, frameHeight:Number, renderInterval:Number, southFirst:Boolean = true):void
		{
			
			_source = source;
			_frameWidth = frameWidth;
			_frameHeight = frameHeight;
			_renderInterval = renderInterval;
			_southFirst = southFirst;

			var sourceBd:BitmapData = new BitmapData(source.width, source.height, true, 0x00000000);
			sourceBd.draw(source, null, null, null, null, true);

			var numCols:Number = Math.floor(source.width / frameWidth);
			var numRows:Number = Math.floor(source.height / frameHeight);

			_frameRect = new Rectangle(0, 0, frameWidth, frameHeight);

			_bitmapArray = new Array2(numCols, numRows);

			for (var i:int = 0; i < numRows; i++)
			{
				for (var j:int = 0; j < numCols; j++)
				{
					var bd:BitmapData = new BitmapData(frameWidth, frameHeight, true, 0x000000);
					_frameRect.x = j * frameWidth;
					_frameRect.y = i * frameHeight;

					bd.copyPixels(sourceBd, _frameRect, new Point(0, 0));

					_bitmapArray.set(j, i, bd);

				}

			}
			
			_frameRect.x = 0;
			_frameRect.y = 0;
			
			//if the south faces is on the last row, move it to the first
			if (!_southFirst)
			{
				_bitmapArray.shiftDown();
			}

			setRow(0);
			idle();

			_oldTime = getTimer();
			//addEventListener(Event.ENTER_FRAME, onEnterFrame);
			
		}
		
		/**
		 * Set the state to idle
		 */
		public function idle():void
		{
			_isIdle = true;
			_isDirty = true;
		};
		
		/**
		 * Set the state to walk/animated
		 * 
		 */
		public function action():void
		{
			_isIdle = false;
			_isDirty = true;
		};

		public function everyFrame(e:Event = null):void
		{
			elapsed = getTimer() - _oldTime;
			if (elapsed >= _renderInterval && _isDirty)
			{
				render();
				_oldTime = getTimer();
			}
		}

		public function render():void
		{
			if (_frameBitmap == null)
			{
				_frameBitmap = new Bitmap(new BitmapData(_frameWidth, _frameHeight, true, 0x00000000));
				addChild(_frameBitmap);
			}

			_frameBitmap.bitmapData.lock();
			
			if (_isIdle)
			{
				_currentColNum = 0;
				_frameBitmap.bitmapData.copyPixels(_currentRow[_currentColNum], _frameRect, new Point(0, 0), null, null, false);
				//trace(this + "render idle");
				_isDirty = false;
			}
			else
			{
				
				if (_currentColNum < _currentRow.length-1)
				{
					try {
						_frameBitmap.bitmapData.copyPixels(_currentRow[_currentColNum], _frameRect, new Point(0, 0), null, null, false);
					}catch (error:Error) {
						//trace("ERROR RENDERING : " + _currentColNum);
						return;
					}
					_currentColNum++;
				}
				else
				{
					_currentColNum = 1;
				}
				
				_isDirty = true;
			}
			
			_frameBitmap.bitmapData.unlock();
		}

		/**
		 * Set the active direction and make _currentRow point to a specific row in _bitmapArray
		 * @param	direction
		 */
		public function setRow(rowNo:int):void
		{
			//trace(this +"set direction : " +direction);
			_direction = rowNo;
			
			//trace(this + "setDirection, row " + rowNum);
			_currentRow = _bitmapArray.getRow(_direction);
			
			_isDirty = true;
			_isIdle = false;
			render();
			
		}
		
		/**
		 * Get currently rendered frame
		 * 
		 * @return Bitmap
		 */
		public function getFrameBitmap():Bitmap
		{
			return _frameBitmap;
		}
		
		/**
		 * Get current direction
		 * @return
		 */
		public function getDirection():int
		{
			return _direction;
		};

	}

}

I have changed a few things in this. Like Instead of using static value and strings, I am using int. setRow function is completely mine. I am not sure about the authenticity of this code. Can somebody take a peek and tell me if this is the best bitmap blitting? If something can be changed to make it faster let me know. Thanks.

Edit:- I also read Native code is faster than AS3. What does this mean? Here is the link
Also it seems I cant access stage3d. Do I need to download some packages for it?

 
Flag Post

Topic: Game Programming / Better solution

I have a C++ background. Any tips, advices, hints, shortcuts or performance boosting methods in as3 will be appreciated.

 
Flag Post

Topic: Game Programming / Better solution

Ah thanks for all that.

So this

var e:Enemy;
e = enemyArr[i];
e.move();

better than

Enemy(enemyArr[i]).move();

?

Edit:- I read that

arr[i] = something;

is faster than

arr.push(something);

does that hold true for a vector as well?

 
Flag Post

Topic: Game Programming / Write a game in flash[haxe] and compile that same code to windows, linux, mobile iOS, flash, and Android into native code

Wow this sounds awesome. Whats flash[haxe]? Is it API or another IDE? Please tell when you get the android and iPhone version working and your experience managing it. If its easy enough I wanna make games for all the platforms mentioned :)

 
Flag Post

Topic: Game Programming / Better solution

That while loop looks so much better than my for loop but are while loops faster than for loops?

Err toggle the enterframe? Whats that?

I plan to put a lot more stuff into enemy movement function, like flocking toward player or moving away from player. So I think its justified to use a function, right?

Lastly I dont really know what a vector is. Could you please explain?

 
Flag Post

Topic: Game Programming / Better solution

Alright. I tested with the backward iterating and it doesnt throw an error. But I will like to know why you recommend having an additional loop to check if enemy is dead or not. I think that would be more cpu intensive than calculating array length everytime in first loop itself? Please explain. Thanks.

 
Flag Post

Topic: Game Programming / Better solution

Thanks for quick reply. Remove differently, how? And iterate backwards like use — instead of ++?

 
Flag Post

Topic: Game Programming / Better solution

The code tags doesnt work. How do you show code?

EDIT:- hope that works. How do you bold the thing in pre tags?
EDIT2:- There seems much better now. I used pre tags on both classes. Hope it is readable.

 
Flag Post

Topic: Game Programming / Better solution

Hey all, I am working on my first game and I think I am doing something badly. I would appreciate if anyone can tell me a better solution to following code:-

Game.as

//import shit omitted
//handleGame function is the game loop (enterframe, called every frame, watever)
function handleGame(evt:Event)
{
	//trace("pause is " + pause);
	if (!pause)
	{
		checkInt = enemyArr.length;
		//trace("moving enemy");
                //*This is the problem in short*
                //I read this calculates the enemyArr length everytime 
                //and is a bad practice. but when I try to use checkInt 
                //instead it throws a null error because midway the length
                // of enemyArr changes
		for (var i:int = 0; i < enemyArr.length; i++)          
		{
			//trace("ai shit "+i);
			Enemy(enemyArr[i]).move();
		}
		//trace("after moving");
	}
}

//removeDeadAI function removes the dead ai
function removeDeadAI(id:int)
{
	//trace("removing enemy arr length "+enemyArr.length);
	for (var i:int = 0; i < enemyArr.length; i++)
	{
		if (enemyArr[i].id == id)
		{
			enemyArr.splice(i, 1);
			break;
		}
	}
	//trace("after removing arr len" + enemyArr.length);
}

//rest code omitted

Enemy.as

//import shit omitted
//move function handles the enemy movement and also check its health
function move():void
{
          this.x+=speed;
          if(this.health <= 0)
                 die();
}

//die function clears the things and remove from parent
function die()
{
	//trace("removing stuff");
	gameRef.removeDeadAI(id);        //id is unique to each enemy
	this.parent.removeChild(this);
}

This works but I would like to know how to pre calculate the length and then run the for loop in game class to move the enemy. Any hints are appreciated. Also how to splice the correct instance of enemy without looping through all the elements?

All object/variables have been defined. So don’t worry about it.