A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: How do i use setInterval

  1. #1
    Ozzy the Grey
    Join Date
    Sep 2007
    Posts
    61

    How do i use setInterval

    i use as2 and i want to know how to use setInterval
    Code:
    var pistolshottime:Number = 1
    var counter:Number = 0;
    var interval:Number = setInterval(pistolshottimer(),1000);
    
    
    var pistolshottimer : function(Void){
    	counter += 1;
    	if (counter == pistolshottime) {
    		clearInterval(interval);
    		counter = 0;
    	}
    }
    pistolshottimer is the period between shots and i just want to know why this doesnt work it says "an identifier is expected after the ':'" and it says after pistolshottimer

  2. #2
    Senior Member dudeqwerty's Avatar
    Join Date
    Mar 2005
    Location
    Bosnia
    Posts
    1,626
    You're (nearly) using set interval correctly, but its your function declaration which is mostly off:
    Code:
    var pistolshottime:Number = 1
    var counter:Number = 0;
    var interval:Number = setInterval(pistolshottimer,1000);
    
    
    function pistolshottimer(Void){
    	counter += 1;
    	if (counter == pistolshottime) {
    		clearInterval(interval);
    		counter = 0;
    	}
    }
    New sig soon

  3. #3
    Senior Member dudeqwerty's Avatar
    Join Date
    Mar 2005
    Location
    Bosnia
    Posts
    1,626
    Oh, and If you want to declare a function in "variable style" declaration, do it like this:
    Code:
    var myFunc:Function = function():Void {
    	trace("myFunc called");
    }
    
    myFunc();
    Putting Void in the arguments list in a function declaration in AS2 doesn't do anything. "Void" will just become another parameter for the function.
    New sig soon

  4. #4
    Ozzy the Grey
    Join Date
    Sep 2007
    Posts
    61
    Code:
    var pistolshottime:Number = 1;
    var counter:Number = 0;
    var interval:Number = setInterval(pistolshottimer, 1000);
    
    
    function pistolshottimer() {
    	counter++;
    	if (counter == pistolshottime) {
    		clearInterval(interval);
    		counter = 0;
    	}
    }
    do i need to put and updateAfterEvent(); or what because i made a dynamic text box and it wont count up whats the deal?

    *EDIT* nevermind it just resets when it hits 1
    Last edited by OzzyGreyman; 09-19-2009 at 03:44 PM.

  5. #5
    Ozzy the Grey
    Join Date
    Sep 2007
    Posts
    61
    how do i set the interval and stuff from an mc i want to make it when it shoots the it will start the interval and set a boolean that it is shooting and after 1 second it will change to false so it can then shoot again

  6. #6
    Ozzy the Grey
    Join Date
    Sep 2007
    Posts
    61
    Code:
    if (Key.isDown(Key.SPACE) and this.pistolammo>0) {
    		if (this.shooting == false) {
    			_root.attachMovie("pistolbullet","pistolbullet"+this.pistolammo,this.pistolammo*50);
    			_root["pistolbullet"+this.pistolammo]._x = this._x;
    			_root["pistolbullet"+this.pistolammo]._y = this._y;
    			_root["pistolbullet"+this.pistolammo]._rotation =  this._rotation-90;
    			this.pistolammo -= 1;
    			_root.interval = setInterval(_root.pistolshottimer, 1000);
    		}
    }
    okay i have it shoot a bullet if shooting is false and start the interval time which should make shooting true but for some reason i can keep shooting what is the problem?

    Code:
    var pistolshottime:Number = 2;
    var counter:Number = 0;
    var interval:Number;
    function pistolshottimer() {
    	counter++;
    	_root.player.shooting == true;
    	if (_root.counter == _root.pistolshottime) {
    		clearInterval(_root.interval);
    		_root.counter = 0;
    		_root.player.shooting == false;
    	}
    }

  7. #7
    Senior Member dudeqwerty's Avatar
    Join Date
    Mar 2005
    Location
    Bosnia
    Posts
    1,626
    == is used for comparison and = is used for assignment.

    So change these lines:
    Code:
    _root.player.shooting == false;
    
    and 
    
    _root.player.shooting == true;
    to:
    Code:
    _root.player.shooting = false;
    
    and
    
    _root.player.shooting = true;
    New sig soon

  8. #8
    Ozzy the Grey
    Join Date
    Sep 2007
    Posts
    61
    thank you but for some reason it still doesn't work i can still just keep shooting and it is doing the counter right but for some reason it doesn't pay attention that shooting HAS TO BE FALSE and it just keeps going

    Code:
    if (Key.isDown(Key.SPACE) and this.pistolammo>0) {
    		if (this.shooting == false) {
    			_root.attachMovie("pistolbullet","pistolbullet"+this.pistolammo,this.pistolammo*50);
    			_root["pistolbullet"+this.pistolammo]._x = this._x;
    			_root["pistolbullet"+this.pistolammo]._y = this._y;
    			_root["pistolbullet"+this.pistolammo]._rotation = this._rotation-90;
    			this.pistolammo -=1
    			_root.interval = setInterval(_root.pistolshottimer, 1000);
    		}
    	}
    Code:
    var pistolshottime:Number = 2;
    var counter:Number = 0;
    var interval:Number;
    
    
    function pistolshottimer() {
    	counter++;
    	_root.player.shooting = true;
    	if (_root.counter == _root.pistolshottime) {
    		clearInterval(_root.interval);
    		_root.counter = 0;
    		_root.player.shooting = false;
    	}
    }
    does it matter what the variable interval is , do i have to reset it to 0 or something?

    *edit* i figured it out, see i was making shooting true in the function an instead i changed it to make it true right after i call the function and now it works perfect
    Last edited by OzzyGreyman; 09-19-2009 at 04:25 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