A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Flash CS4 using AS2: bullet issues

  1. #1
    Member
    Join Date
    Jun 2010
    Posts
    32

    Flash CS4 using AS2: bullet issues

    alright so i have two classes, character (instance name "man") and bullet (instance name "Bullet+_root.getNextHighestDepth") and what happens is if the player turns before his bullet is off the screen, the bullet changes direction as well. I know WHY this happens, i just don't know how i could solve it. My basic idea is to set a variable equal to the scale upon creation of the bullet, but i have no idea how i would do that. Here are my scripts for the character and bullet classes respectively
    Code:
    class character extends MovieClip
    {
    	var jump
    	var gun
    	var shooter
    	function onLoad()
    	{
    		jump = 0
    		gun = 0
    		shooter = 0
    	}
    	function onEnterFrame() 
    	{
                 shooter += 1
                 if(Key.isDown(Key.RIGHT)){_x += 5; _xscale = 100}
                 if(Key.isDown(Key.LEFT)){_x -= 5; _xscale = -100}
                 if(Key.isDown(Key.UP) && jump < 10){_y -= 20; jump += 1}
                 if(this.hitTest(_root.wall0) || this.hitTest(_root.wall1)){_y+= 0; jump = 0} else {_y +=10}
                 if(this.hitTest(_root.gun1)){gun = 2; _root.gun1._alpha =0}
                 if(Key.isDown(Key.SPACE) && shooter > 5 && gun > 1)
                 {
                     shooter = 0
                     var shot = _root.attachMovie("Bullet","Bullet"+_root.getNextHighestDepth(),_root.getNextHighestDepth())
                     shot._x = _x
                     shot._y = _y
            }
        }
    }
    Code:
    class bullet extends MovieClip
    {
        var dir
        function onEnterFrame()
        {
            if(_root.man._xscale > 0){dir = 1}
            if(_root.man._xscale < 0){dir = -1}
            if(dir > 0){_x += 10}
            if(dir < 0){_x -= 10}
            if(_x < -50){this.removeMovieClip()}
            if(_x > 700){this.removeMovieClip()}
        }
    }

  2. #2
    Senior Member
    Join Date
    Jul 2008
    Posts
    391
    Try
    PHP Code:
    class character extends MovieClip
    {
        var 
    jump
        
    var gun
        
    var shooter
        
    function onLoad()
        {
            
    jump 0
            gun 
    0
            shooter 
    0
        
    }
        function 
    onEnterFrame() 
        {
                 
    shooter += 1
                 
    if(Key.isDown(Key.RIGHT)){_x += 5_xscale 100}
                 if(
    Key.isDown(Key.LEFT)){_x -= 5_xscale = -100}
                 if(
    Key.isDown(Key.UP) && jump 10){_y -= 20jump += 1}
                 if(
    this.hitTest(_root.wall0) || this.hitTest(_root.wall1)){_y+= 0jump 0} else {_y +=10}
                 if(
    this.hitTest(_root.gun1)){gun 2_root.gun1._alpha =0}
                 if(
    Key.isDown(Key.SPACE) && shooter && gun 1)
                 {
                     
    shooter 0
                     
    var shot _root.attachMovie("Bullet","Bullet"+_root.getNextHighestDepth(),_root.getNextHighestDepth())
                     
    shot._x _x
                     shot
    ._y _y
                     
    //Added this part to set the bullet's direction only once, right when it's created
                     
    if (_xscale 0) {
                           
    shot.dir 1;
                     } else {
                           
    shot.dir = -1;
                     }
            }
        }

    PHP Code:
    class bullet extends MovieClip
    {
        var 
    dir
        
    function onEnterFrame()
        {
    //I took out the first 2 if statements
            
    if(dir 0){_x += 10}
            if(
    dir 0){_x -= 10}
            if(
    _x < -50){this.removeMovieClip()}
            if(
    _x 700){this.removeMovieClip()}
        }

    I've never worked with classes in AS2 so the syntax might be wrong but this should give you a general idea.

  3. #3
    Member
    Join Date
    Jun 2010
    Posts
    32
    sadly this doesn't work. I'm not clear on the details why, it just doesn't.

Tags for this Thread

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