dcsimg
A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: alternating between functions

  1. #1
    Junior Member
    Join Date
    Sep 2007
    Posts
    2

    alternating between functions

    I am creating a game where the user moves a ball (ball_mc) with the arrow keys or with the a,s,d,w keys.
    This kind of game has been done plenty of times before but i want to switch the way the arrows work every five seconds or so.

    so when the game starts I want the contols function to be active for five seconds and then switch to the switchedControls and let those be active for five seconds, and alternate between the too every five seconds if anyone knows how to do this than help would be greatly appreciated.

    I tried setinterval but i dont think it can be done that way.
    I think i would have to use gettimer but cannot figure out how to get that to work either \

    heres the code for reference

    thanx in advance.



    Code:
    stop();
    
    //----------------------------FUNCTIONS---------------------------\\
    var ballSpeed:Number = 5;
    
    function moveRight () {
    	if (ball_mc._x>0 && ball_mc._x<550) {
    		ball_mc._x += ballSpeed;
    	}
    }
    function moveLeft () {
    	if (ball_mc._x>0 && ball_mc._x<550) {
    		ball_mc._x -= ballSpeed;
    	}
    }
    function moveUp () {
    	if (ball_mc._y>0 && ball_mc._y<400) {
    		ball_mc._y -= ballSpeed;
    	}
    }
    function moveDown () {
    	if (ball_mc._y>0 && ball_mc._y<400) {
    		ball_mc._y += ballSpeed;
    	}
    }
    //----------------------------FUNCTIONS---------------------------\\
    
    //----------------------------KEY PRESS FUNCTIONS---------------------------\\
    var keyListener:Object = new Object();
    Key.addListener(keyListener);
    
    function controls () {
    	keyListener.onKeyDown = function () {
    		onEnterFrame = function  () {
    			if(Key.isDown(Key.RIGHT)) {
    				moveRight();
    			}
    			else if(Key.isDown(Key.LEFT)) {
    				moveLeft();
    			}
    			else if(Key.isDown(Key.UP)) {
    				moveUp();
    			}
    			else if(Key.isDown(Key.DOWN)) {
    				moveDown();
    			}
    			else if(Key.isDown(68)) {
    				moveRight();
    			}
    			else if(Key.isDown(65)) {
    				moveLeft();
    			}
    			else if(Key.isDown(87)) {
    				moveUp();
    			}
    			else if(Key.isDown(83)) {
    				moveDown();
    			}
    		}
    	}
    }
    
    function switchedControls () {
    	keyListener.onKeyDown = function () {
    		onEnterFrame = function  () {
    			if(Key.isDown(Key.RIGHT)) {
    				moveLeft();
    			}
    			else if(Key.isDown(Key.LEFT)) {
    				moveRight();
    			}
    			else if(Key.isDown(Key.UP)) {
    				moveDown();
    			}
    			else if(Key.isDown(Key.DOWN)) {
    				moveUp();
    			}
    			else if(Key.isDown(68)) {
    				moveLeft();
    			}
    			else if(Key.isDown(65)) {
    				moveRight();
    			}
    			else if(Key.isDown(87)) {
    				moveDown();
    			}
    			else if(Key.isDown(83)) {
    				moveUp();
    			}
    		}
    	}
    }
    //----------------------------KEY PRESS FUNCTIONS---------------------------\\

  2. #2
    Professional Air Guitarist Frag's Avatar
    Join Date
    Dec 2002
    Location
    Brain, Body, Clothes, Computer Chair, Room, House, Neighborhood, City, State, Country, Continent, World, Galaxy, Universe, and on?
    Posts
    811
    PHP Code:
    var switchNum:Number;
    function 
    switcher() {
    switchNum++;
    if (
    switchNum%2) {
    controls();
    } else {
    switchedControls();
    }
    }
    setInterval(switcher5000); 
    That should call controls() after 5 seconds, then switchedControls() 5 seconds after that, then controls() again after 5 more seconds, and so on...

  3. #3
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967
    Yup, and both your controls and switchcontrols functions would have very similar content. First they would delete the existing events for the keypresses, and then they would create the new ones.

    So you should probably add a line like:
    delete keyListener.onKeyDown;

    to the front. My syntax could easily be wrong on delete. Not sure if it needs () or not. I forget.

  4. #4
    Junior Member
    Join Date
    Sep 2007
    Posts
    2
    thanks this was exactly what i was looking for thanx

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