Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Book: Professional Flash Mobile Development: Creating Android and iPhone Applications
This is the forum to discuss the Wrox book Professional Flash Mobile Development: Creating Android and iPhone Applications Richard Wagner ; ISBN: 978-0-470-62007-6
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Book: Professional Flash Mobile Development: Creating Android and iPhone Applications section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old October 18th, 2012, 05:51 PM
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.
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Two Variable & Function optimization questions Asymmetricalman Book: Professional Flash Mobile Development: Creating Android and iPhone Applications 0 October 18th, 2012 05:44 PM
send variable in function to another function schoolBoy Javascript How-To 6 March 3rd, 2007 08:16 AM
Questions about Inheritance & Polymorphism jimblanc Java Basics 1 December 31st, 2006 02:54 PM
I Need Interview Questions & Answers ... keyur_rhtdm .NET Framework 2.0 2 August 5th, 2006 10:55 AM
passing variable to function Boszky Pro PHP 2 December 15th, 2005 02:01 AM



All times are GMT -4. The time now is 09:53 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.