A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Script dont run smooth on mobile but in the browser it does?

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    13

    Script dont run smooth on mobile but in the browser it does?

    Hello People I hope this is the right section for my problem.

    I have a little jump and run game that i bought from cartonsmart.com

    I rebuilt it so that i can play it with my mobile....

    now my problem is that it runs perfect in the browser very smooth and everything
    but on my mobile its very slow and raking not at all smooth.

    what is the problem can you help me...i think it got something to do with the "doesTheWorldNeedToScroll" code

    Im a grafic Designer not a programmer so sorry for the question

    Code:
    package 
    {
    
    	import flash.display.MovieClip;
    	import flash.events.TimerEvent;
    	import flash.utils.Timer;
    	import MainTimer;
    	import flash.events.Event;
    	import flash.events.KeyboardEvent;
    	import flash.events.TouchEvent;
    	import flash.ui.Multitouch;
    	import flash.ui.MultitouchInputMode;
    
    	public class DocumentMain extends MovieClip
    	{
    
    		//initial variables
    		private var currentNumberOfEnemiesOnstage:int;
    		private var initialNumberOfEnemiesToCreate:int = 1;
    		private var maxNumberOfEnemiesOnstage:int = 2;
    
    
    		private var ninjaKills:int;
    		private var childToRemove:int;
    		private var level:int = 1;
    		private var e:int = 0;
    		private var minute:int = 0;
    		private var second:int = 60;
    		private var gameTimer:MainTimer;
    		private var childrenOnStage:int;
    		private var lastX:int;// variable to determine where the last x of the player was.
    
    		private var thePlayer:Player;
    		private var theEnemy:Enemy;
    
    		private var doesTheWorldNeedToScroll:Boolean;
    
    		private var makeNewEnemyTimer:Timer = new Timer(3000,1);
    		private var finishOffEnemy:Timer = new Timer(500,1);
    
    		public function DocumentMain()
    		{
    			// constructor code
    
    			trace("document main initiated");
    
    			makeNewEnemyTimer.addEventListener(TimerEvent.TIMER_COMPLETE,makeNewEnemyHandler);
    			makeNewEnemyTimer.start();
    
    			gameTimer = new MainTimer(minute,second);
    
    			addChild(gameTimer);
    			gameTimer.x = 60;
    			gameTimer.y = 40;
    
    			thePlayer = new Player  ;
    			addChild(thePlayer);
    			thePlayer.x = stage.stageWidth * 0.5;// halfway across the stage
    			thePlayer.y = 0;
    			thePlayer.name = "player";
    
    
    			while (e < initialNumberOfEnemiesToCreate)
    			{
    
    				createEnemy();
    				e++;
    			}
    
    			ninjaKills = 0;
    			lastX = thePlayer.x;
    
    			childrenOnStage = this.numChildren;//make sure you assign this value after adding children
    
    			this.addEventListener(Event.ENTER_FRAME,mainGameLoop);
    
    			stage.focus = stage;
    Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
    
    			stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
    			stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
    			rechts.addEventListener(TouchEvent.TOUCH_BEGIN,rechts_gehen);
    			rechts.addEventListener(TouchEvent.TOUCH_END,rechts_gehen_OUT);
    			links.addEventListener(TouchEvent.TOUCH_BEGIN,links_gehen);
    			links.addEventListener(TouchEvent.TOUCH_END,links_gehen_OUT);
    			jump.addEventListener(TouchEvent.TOUCH_BEGIN,jump_gehen);
    			jump.addEventListener(TouchEvent.TOUCH_END,jump_gehen_OUT);
    			
    			
    			
    		}
    function rechts_gehen(e:TouchEvent):void
    	{
    		//thePlayer.moveRight = true;
    		thePlayer.moveRight();
    
    		/*if (e.target == rechts)
    		{
    		trace("working");
    		hero.moveRight = true;
    		}
    		*/
    
    	}
    
    function rechts_gehen_OUT(e:TouchEvent):void
    	{
    
    		//thePlayer.moveRight = false;
    		thePlayer.standStill();
    	}
    
    
    	function links_gehen(e:TouchEvent):void
    	{
    		if (e.target == links)
    		{
    			trace("working");
    			thePlayer.moveLeft();
    		}
    	}
    
    	function links_gehen_OUT(e:TouchEvent):void
    	{
    
    		thePlayer.standStill();
    	}
    
    	function jump_gehen(e:TouchEvent):void
    	{
    		if (e.target == jump)
    		{
    			trace("working");
    			thePlayer.startJumping();
    		}
    	}
    
    	function jump_gehen_OUT(e:TouchEvent):void
    	{
    
    		//thePlayer.startJumping = false;
    	}
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    		private function keyDownHandler(e:KeyboardEvent):void
    		{
    
    			switch (e.keyCode)
    			{
    
    				case 37 ://left
    					thePlayer.moveLeft();
    					break;
    				case 38 ://up
    					thePlayer.startJumping();
    					break;
    				case 39 ://right
    					thePlayer.moveRight();
    					break;
    				case 40 ://down to attack
    				thePlayer.attack();
    				break;
    			}
    
    
    		}
    		private function keyUpHandler(e:KeyboardEvent):void
    		{
    
    			switch (e.keyCode)
    			{
    
    				case 37 ://left
    				case 39 ://right
    
    
    					thePlayer.standStill();
    					break;
    					
    				case 40 ://down to attack
    					//as a reminder, you COULD do something when you finish attacking.
    				break;
    
    				default :
    					//anything
    
    			}
    
    
    		}
    
    
    		private function mainGameLoop(event:Event):void
    		{
    
    			gameResets();
    
    			removeOrCreateNewEnemies();
    
    			processCollisions();
    
    			scrollStage();
    
    			// ends  mainGameLoop
    		}
    
    		private function createEnemy():void
    		{
    
    			theEnemy = new Enemy( (Math.random() * 5 ) + 1  )  ;
    			addChild(theEnemy);
    			theEnemy.x = Math.random() * stage.stageWidth + stage.stageWidth / 2;// anywhere across the stage
    			theEnemy.y = 0;
    			theEnemy.name = "enemy";
    
    			childrenOnStage = this.numChildren;
    
    		}
    
    		private function removeOrCreateNewEnemies():void
    		{
    
    			for (var c:int = 0; c < childrenOnStage; c++)
    			{
    
    				if (getChildAt(c).name == "enemy" && getChildAt(c).y > stage.stageHeight)
    				{
    
    					removeChildAt(c);
    					createEnemy();
    				}
    				if (getChildAt(c).name == "enemy" && getChildAt(c).x < thePlayer.x - stage.stageWidth)
    				{
    					removeChildAt(c);
    					createEnemy();
    				}
    
    
    			}
    
    
    		}
    
    		private function makeNewEnemyHandler(event:TimerEvent):void
    		{
    
    
    			currentNumberOfEnemiesOnstage = 0;
    
    			for (var c:int = 0; c < childrenOnStage; c++)
    			{
    
    				if (getChildAt(c).name == "enemy")
    				{
    					currentNumberOfEnemiesOnstage++;
    
    				}
    
    			}
    
    			if (currentNumberOfEnemiesOnstage < maxNumberOfEnemiesOnstage)
    			{
    
    				trace("not enough enemies onstage, make more");
    				createEnemy();
    			}
    
    			makeNewEnemyTimer.start();
    
    		}
    		
    		public function finishOffEnemyComplete(event:TimerEvent):void {
    			
    			ninjaKills ++;
    			killScoreBox.text = String ( ninjaKills) + " KILLS"; 
    			
    			removeChildAt ( childToRemove);
    			
    			childrenOnStage = this.numChildren;
    			
    		}
    		
    
    
    		private function processCollisions():void
    		{
    
    
    			for (var c:int = 0; c < childrenOnStage; c++)
    			{
    
    				if (getChildAt(c).name == "player" || getChildAt(c).name == "enemy")
    				{
    
    					if (_boundaries.hitTestPoint(getChildAt(c).x,getChildAt(c).y,true))
    					{//if the boundary collides with the player or enemy
    
    						while (_boundaries.hitTestPoint(getChildAt(c).x,getChildAt(c).y,true))
    						{
    
    							BoundaryObject(getChildAt(c)).incrementUpward();
    							//bump up the object until it isn't hitting the boundary;
    
    							if (_boundaries.hitTestPoint(getChildAt(c).x,getChildAt(c).y,true))
    							{
    								// do nothing
    
    							}
    							else
    							{// once it isn't hitting the boundary, do this function for keeping the object on the boundary
    								BoundaryObject(getChildAt(c)).keepOnBoundary();
    
    							}
    
    
    
    						}
    						//  ends while (  _boundaries.hitTestPoint ( getChildAt(c).x , getChildAt(c).y, true) ) {;
    
    					}// ends if ( _boundaries.hitTestPoint ( getChildAt(c).x , getChildAt(c).y, true) )
    				}//ends if ( getChildAt(c).name == "player") 
    				////////////////////////////////////////////////////////////////////////
    				////////////////////////////////////////////////////////////////////////
    				////////////////////////Collision with ENEMIES////////////////
    
    				if (getChildAt(c).name == "enemy")
    				{
    					if( getChildAt(c).hitTestPoint ( thePlayer.x, thePlayer.y , true) )
    					{
    						
    						if ( thePlayer.isAttacking == false ) {
    							
    							// we are being attacked (and not defending)
    							
    							health.width = health.width -2;
    							Enemy(getChildAt(c)).makeEnemyAttack();
    							
    						} else {
    							
    							// we are attacking that enemy
    							childToRemove = c;
    							Enemy(getChildAt(c)).makeEnemyDie();
    							
    							finishOffEnemy.start();
    							finishOffEnemy.addEventListener(TimerEvent.TIMER_COMPLETE, finishOffEnemyComplete);
    							
    						}
    						
    						
    					} // ends hitTestPoint
    					
    					else if ( Enemy(getChildAt(c)).enemyIsAttacking == true )  { 
    						 //if there isn't a collision between player and enemy, BUT the enemy is attacking
    						 Enemy(getChildAt(c)).makeEnemyStopAttacking();
    						
    						
    					}
    					
    
    				}//if (getChildAt(c).name == "enemy") 
    			}//ends the for loop
    
    
    
    
    
    			//ends processCollisions
    		}
    
    
    
    		private function gameResets()
    		{
    
    			if (gameTimer.timerHasStopped == true)
    			{
    
    				resetBoard();
    			}
    			else if (thePlayer.y > stage.stageHeight)
    			{
    
    				resetBoard();
    			}
    			else if (_boundaries.pole.hitTestPoint(thePlayer.x,thePlayer.y,true) && doesTheWorldNeedToScroll == false)
    			{
    
    				level++;
    
    				_boundaries.nextFrame();
    
    				resetBoard();
    			}
    			else if (health.width <= 2)
    			{
    
    				resetBoard();
    			}
    
    
    			// ends  gameResets
    		}
    
    		private function resetBoard():void
    		{
    
    			health.width = 300;
    			thePlayer.x = stage.stageWidth * 0.5;
    			_boundaries.x = stage.stageWidth * 0.5;
    			sky.x = stage.stageWidth * 0.5;
    			thePlayer.y = 0;
    			_boundaries.y = 0;
    			sky.y = 0;
    			ninjaKills = 0;
    
    			gameTimer.resetTimer(0,15);
    
    		}
    
    		private function scrollStage():void
    		{
    			if (thePlayer.x != lastX)
    			{
    
    				doesTheWorldNeedToScroll = true;
    			}
    			else if (thePlayer.x == lastX)
    			{
    
    				doesTheWorldNeedToScroll = false;
    
    			}
    
    			if (doesTheWorldNeedToScroll == true)
    			{
    
    
    				sky.x +=  stage.stageWidth * 0.5 - thePlayer.x * 1.002;
    
    
    				for (var b:int = 0; b < childrenOnStage; b++)
    				{
    
    					if (getChildAt(b).name == "enemy")
    					{
    
    						getChildAt(b).x +=  stage.stageWidth * 0.5 - thePlayer.x;
    					}
    
    
    				}
    
    				_boundaries.x +=  stage.stageWidth * 0.5 - thePlayer.x;
    
    			}
    			else
    			{
    
    				sky.x -=  0.5;
    			}
    
    			// RUN THIS FOLLOWING LINE LAST
    			thePlayer.x = stage.stageWidth * 0.5;
    
    			lastX = thePlayer.x;
    
    		}
    
    
    	}
    
    }
    Attached Files Attached Files

  2. #2
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,145
    It has to be coded for mobile. It has to follow the rules for mobile optimization.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    13
    Thank you for your awnser.

    Is there a tutorial where i can code this for mobile? or what must i do to turn the code into a mobile version?
    I thought i coded it for mobile....by puting multitouch in it?

  4. #4
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,145
    That just enables touch. The big issue is getting the graphics to show. Regular flash targets the cpu, which is really weak in mobile devices. You have to convert everything into bitmaps to make use of the gpu in mobile devices (works for desktop too).

    You can draw things in flash just like usual, but you have to convert the image to bitmaps. Moving a bitmap around the screen is easy and smooth on mobile devices.

    Keep all the bitmaps you create smaller than the dimensions of the device. Break big images into tiles.

    Google for: as3 mobile bitmaps

    gotoandlearn.com has great tutorials on everything in flash including making games.
    Last edited by moot; 12-02-2013 at 06:52 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center