View Single Post
  #1 (permalink)  
Old October 18th, 2012, 05:51 PM
Asymmetricalman Asymmetricalman is offline
Registered User
Points: 10, Level: 1
Points: 10, Level: 1 Points: 10, Level: 1 Points: 10, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Two Variable & Function optimization questions

/*I have a couple of questions reguarding streamlining code for an iOS game. I am quite green to this and would really appreciate some good input.

The game structure is based on an MVC model. With a MVC system for the general game
one for the main Character, and one for each level.


Questions 1):
Sometimes the level Controllers must talk to the Character. I have the level model and the Charcater model instantiated with the Game Model to enable this.
If a value is being used often, ans stays consistent for the game is it more efficient for performance to call the variable directly or make another variable that can
be used for it during the game.


For example should I use:
Code:
	var _currentX:int; //ignore this one. Only used for context
	_currentX =  _levelModel.gameModel.player;
or instead use:
Code:
	var _currentX:int; 
	var _player:Object = _levelModel.gameModel.player;
	_currentX = _player.xPos;


Questions 2): Along these lines I had originally used a static function to bounce the character off the walls. It was code also called every Frame.
I then realized that many of the variable it needed were also in used in other similar code, so I figured less variables is better, and I put them all
as public variables at the top of a class that many functions can use them. That way they are only instantiated once. Yet now I have read that static
functions are more efficient. How can this be when is seems to be creating these variables every Frame;
Should I stick with my original exclusive Static Function below, or use the following function that uses variables that can be shared?


//Static function
Code:
		
		//Bounce object at stage edges
		static public function bounce(object:Object, stage:Object):void
		{
			var objectWidthHalf:int = object.width* 0.5;
			var objectHeightHalf:int = object.height* 0.5;
			var stageWidth:int = stage.stageWidth;
			var stageHeight:int = stage.stageHeight;
			
			//x-axis
			if (object.xPos + (objectWidthHalf) > stageWidth)
			{
				object.setX = stageWidth - (objectWidthHalf);
				object.vx *= -1;
			}
			else if (object.xPos - (objectWidthHalf) < 0)
			{
				object.setX = 0 + (objectWidthHalf);
				object.vx *= -1;
			}
			
			//y axis
			if (object.yPos - (objectHeightHalf) < 0)
			{
				object.setY = 0 + (objectHeightHalf);
				object.vy *= -1;
			}
			else if (object.yPos + (objectHeightHalf) + 80 > stageHeight)
			{
				object.setY = stageHeight - (objectHeightHalf) - 80;
				object.vy *= -1;
			}
		}



//Class that holds the function (as well as a number for others not shown that use the same variables)


Code:
package Control
{
	import flash.events.Event;
	import flash.display.Sprite;

	public class ScrollController
	{
		//Variable to hold a reference to the game player
		private var _levelModel:Object;
		
		private var _player:circleModel;
		private var _background:Sprite;
		
		//stage size
		private var stageWidth:uint;
		private var stageHeight:uint;
		

		public function ScrollController(levelModel:Object):void
		{
			_levelModel = levelModel;
			initialize();
		}
		
		
		private function initialize():void
		{
			//stage size
			stageWidth = _levelModel.gameModel.STAGE_WIDTH;
			stageHeight = _levelModel.gameModel.STAGE_HEIGHT;
			
			//objects
			_player = _levelModel.gameModel.gopher;
			_background = _levelModel.backgroundImage;
		}

		
		//Bounce object at stage edges
		public function stageBoundariesBounce():void
		{
			//set up character dimensions as it changes with gameScale
			_playerWidthHalf = _player.width * 0.5;
			_playerHeightHalf = _player.width * 0.5;
			
			
			if (_player.xPos + (_playerWidthHalf) > stageWidth)
			{
				_player.setX = stageWidth - (_playerWidthHalf);
				_player.vx *= -1;
			}
			else if (_player.xPos - (_playerWidthHalf) < 0)
			{
				_player.setX = 0 + (_playerWidthHalf);
				_player.vx *= -1;
			}
			
			
			if (_player.yPos - (_playerHeightHalf) < 0)
			{
				_player.setY = 0 + (_playerHeightHalf);
				_player.vy *= -1;
			}
			else if (_player.yPos + (_playerHeightHalf) + 80 > stageHeight)
			{
				_player.setY = stageHeight - (_playerHeightHalf) - 80;
				_player.vy *= -1;
			}
		}
		
		
	}
}
I realize this is a lot to throw out there, and really appreciate the input.

sincerely;

*/

Last edited by Asymmetricalman; October 25th, 2012 at 09:45 AM..