A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 25

Thread: [RESOLVED] [AS2.0] Can't remove enemy?

  1. #1
    Flash Newbie
    Join Date
    Mar 2009
    Location
    UK
    Posts
    32

    resolved [AS2.0] Can't remove enemy

    Code:
    // Cycle through bullets and move them
         for (i=0; i < bullets.length; i++)
         {
              cb = bullets[i];
              cb._x += cb.xs;
              cb._y += cb.ys;	
    // Bullet hits enemy1, 50 points
    	if (_root.cb.hitTest(_root.happy)) {
    		_root["happy"].removeMovieClip();
    		_root.score += 50;
    		} 
    	 }
    	if (_root.ship.hitTest(_root.happy)) {
    		_root.removeMovieClip(_root.happy);
    		_root.life--;
    		
    		}	
    	 if (_root.life == 0 ){
    		nextFrame();
    		}
    	}
    If i shoot at my enemy, the enemy is not removed when hit and it won't remove when the ship collides with it... what am I doing wrong?
    Last edited by mt1; 03-19-2009 at 04:43 PM.

  2. #2
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Did you attach enemy movie clip through code or did you place it on stage manually? If its placed on stage in Flash IDE then you need to use swapDepths first:

    _root.happy.swapDepths(1000);
    _root.happy.removeMovieClip();

  3. #3
    Flash Newbie
    Join Date
    Mar 2009
    Location
    UK
    Posts
    32
    Quote Originally Posted by tonypa View Post
    Did you attach enemy movie clip through code or did you place it on stage manually? If its placed on stage in Flash IDE then you need to use swapDepths first:

    _root.happy.swapDepths(1000);
    _root.happy.removeMovieClip();
    I placed it on the stage manually "drag and drop".
    Edit:
    It works when the enemy is hit with the bullet but if the ship collides with enemy then nothing happens? Thanks btw.
    Last edited by mt1; 03-15-2009 at 10:52 AM.

  4. #4
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    You are targeting the _root.happy mc two different ways. Try to use the same one you used in the bullet hitTest and see if that makes a difference.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  5. #5
    Flash Newbie
    Join Date
    Mar 2009
    Location
    UK
    Posts
    32
    Quote Originally Posted by ImprisonedPride View Post
    You are targeting the _root.happy mc two different ways. Try to use the same one you used in the bullet hitTest and see if that makes a difference.
    The problem now is that when the ship collides with the _root.happy mc it doesn't remove it... the bullet hitTest is fine.

    Code:
    // Cycle through bullets and move them
         for (i=0; i < bullets.length; i++)
         {
              cb = bullets[i];
              cb._x += cb.xs;
              cb._y += cb.ys;	
    		  
    // Bullet hits enemy1, swapDepth function enables a movie clip to 
    // change its stacking order, refers to a movie clip stored on the 
    // main timeline, so I need to tell Flash to retrace my steps to 
    // reach the movie clip.
    	if (_root.cb.hitTest(_root.happy)) {
    		_root.happy.swapDepths(1000);
    		_root.happy.removeMovieClip(); 
    		_root.score += 50;
    		}  
    	if (_root.ship.hitTest(_root.happy)) {
    		_root.happy.swapDepths(1000);
    		_root.happy.removeMovieClip(); 
    		_root.life--;
    		}	
    	 if (_root.life == 0 ){
    		nextFrame();
    		}
    	}
    	}

  6. #6
    President PhobiK's Avatar
    Join Date
    Jan 2005
    Location
    Guatemala
    Posts
    918
    I usually write it this way (after the swapDepths): _root.happy.removeMovieClip("");
    Hope it works for you.
    This is MC. His _parents sent him to stop() by the super market to buy some _root beer if he wanted to gotoAndPlay() with his friends at the park later.

    This is my Blog!... The gaming Process
    Please check out my site: Giddel Creations

  7. #7
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    Unless it's absolutely necessary for layering purposes, I generally just use the highest depth in the scope of the mc. In your case I'd replace
    Code:
     _root.happy.swapDepths(1000);
    with
    Code:
    _root.happy.swapDepths(_root.getNextHighestDepth());
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  8. #8
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Maybe your ship vs happy hittest is not working. Does it reduce lives properly?

  9. #9
    Flash Newbie
    Join Date
    Mar 2009
    Location
    UK
    Posts
    32
    Quote Originally Posted by tonypa View Post
    Maybe your ship vs happy hittest is not working. Does it reduce lives properly?
    It was working before I added the swapDepth .... (code below doesn't work... presumebly it has something to do with the layering? Tried taking the red part of the code out but nothing..

    Code:
    	if (_root.ship.hitTest(_root.happy)) {
    		_root.happy.swapDepths(1000);
    		_root.happy.removeMovieClip(); 
    		_root.life--;
    		}
    Edit:
    2nd version (different to post #5)... when _root.happy is hit with the bullet it doesn't vanish, if ship collides with _root.happy it removes the happy mc but only when the score is > 0.

    Code:
    if (_root.cb.hitTest(_root.happy)) {
    		_root.happy.removeMovieClip(); 
    		_root.score += 50;
    		}  
    	if (_root.ship.hitTest(_root.happy)) {
    		_root.happy.swapDepths(1000);
    		_root.happy.removeMovieClip(); 
    		_root.life--;
    		}	
    	 if (_root.life == 0 ){
    		nextFrame();
    		}
    Last edited by mt1; 03-16-2009 at 06:28 AM.

  10. #10
    Flash Newbie
    Join Date
    Mar 2009
    Location
    UK
    Posts
    32
    Quote Originally Posted by ImprisonedPride View Post
    Unless it's absolutely necessary for layering purposes, I generally just use the highest depth in the scope of the mc. In your case I'd replace
    Code:
     _root.happy.swapDepths(1000);
    with
    Code:
    _root.happy.swapDepths(_root.getNextHighestDepth());
    Works the same doesn't it?

  11. #11
    Senior Member
    Join Date
    Oct 2008
    Posts
    120
    If you dragged your mc onto the stage from the library rather then attaching it through code, remove it using:

    unloadMovie(mc);

    always works for me, I hope this helps.

    EDIT: Also, where is this code? Why are all those _root.'s used?
    Last edited by DeadRiver; 03-16-2009 at 07:56 AM.

  12. #12
    Flash Newbie
    Join Date
    Mar 2009
    Location
    UK
    Posts
    32
    Quote Originally Posted by DeadRiver View Post
    If you dragged your mc onto the stage from the library rather then attaching it through code, remove it using:

    unloadMovie(mc);

    always works for me, I hope this helps.

    EDIT: Also, where is this code? Why are all those _root.'s used?
    Code:
    	if (cb.hitTest(happy)) {
    		unloadMovie(happy); 
    		score += 50;
    		}  
    	if (ship.hitTest(happy)) {
    		unloadMovie(happy);
    		life--;
    		}	
    	 if (life == 0 ){
    		nextFrame();
    		}
    	}
    	}
    If i use unloadMovie(), _root.happy is only removed when bullet hits it.... This is in a keyframe in the actions layer. And I have no explanation for the _root stuff... i'll try with out them

    Edit:
    _roots removed from the code, it had no affect on the game.

    My ship and happy are on different layers.. is this the problem? I tried them on the same layer but immediately went to the Game Over frame?
    Last edited by mt1; 03-16-2009 at 08:13 AM.

  13. #13
    Senior Member
    Join Date
    Oct 2008
    Posts
    120
    Thats odd. If you want to post your fla, I'm sure someone could figure it out if not me.
    EDIT: I'm also rly bored at school right now lol. I'd be glad to optimize it as much as possible for you too.
    Last edited by DeadRiver; 03-16-2009 at 08:28 AM.

  14. #14
    Flash Newbie
    Join Date
    Mar 2009
    Location
    UK
    Posts
    32
    Well I can't attach it because it's too big so I'lll have to upload it and you guys will have download it... Here's the link: (The flash edited, it has an intro screen which i took out so you'll see a gap at the start).

    It's in Flash CS3 format.

    *link removed, no longer needed*
    Last edited by mt1; 03-16-2009 at 09:31 AM.

  15. #15
    Senior Member
    Join Date
    Oct 2008
    Posts
    120
    err it's a lot messier than I'm accustomed to lol, but I found the problem.
    I don't know why nobody caught this...

    Just take the ship to happy hitTest code out of the bullets for loop because when it's in the for loop, there has to be at least one bullet mc on the stage in order for the ship to happy hitTest to work.

  16. #16
    Flash Newbie
    Join Date
    Mar 2009
    Location
    UK
    Posts
    32
    Quote Originally Posted by DeadRiver View Post
    err it's a lot messier than I'm accustomed to lol, but I found the problem.
    I don't know why nobody caught this...

    Just take the ship to happy hitTest code out of the bullets for loop because when it's in the for loop, there has to be at least one bullet mc on the stage in order for the ship to happy hitTest to work.
    Yeah sorry... noob at work Originally i had codes everywhere i.e. code attach to various things until i was told that i'd be better having an actions layer lmao.

    And thank you, it has worked! Problem solved.

  17. #17
    Senior Member
    Join Date
    Oct 2008
    Posts
    120

  18. #18
    Flash Newbie
    Join Date
    Mar 2009
    Location
    UK
    Posts
    32
    Where's my problem now guys? I was just mucking about to see how I could add more enemies but again it won't remove them... It just blinks when the bullet hits it and adds 50 to the score, it's always there.... Notice my code is tidier than before

    Code:
    // Set the speed for the object 
    	var ship_speed:Number = 8;
    // Mouse Rotation
    	var rads:Number;
    // Enemy1
    	var happy_radius = 10+(Math.random()*50);
    	var happy_speed =  3+Math.random()*20;
    	var happy_xcenter = happy._x;
    	var happy_ycenter = happy._y;
    	var happy_degree = 0;
    	var happy_radian;
    	xspace = 70;
    	yspace = 60;
    	xstep = 4;
    	ydown = 1;
    // Bullet array/speed
    	var bullets:Array = new Array();
    	var bullet_speed:Number = 20;
    // Score, life
    	score = 0;
    	life = 3;
    	
    	var Background:Sound = new Sound();
        Background.attachSound("Background");
        Background.start();
    	 
    Background.onSoundComplete = function() {
       Background.start();
    }
    
    onEnterFrame = function() {	
    	ShipControls();
    	ShipBoundary();
    	faceMouse(ship);
    	moveToMouse();
    	ShootBullets();
    	Enemy1();
    }
    	
    function ShipControls(){
    	if (Key.isDown(Key.RIGHT)) {
    		ship._x += ship_speed;
    	}
    	else if (Key.isDown(Key.LEFT)) {
    		ship._x -= ship_speed;
    	}
    	if (Key.isDown(Key.UP)) {
    		ship._y -= ship_speed;
    	}
    	else if (Key.isDown(Key.DOWN)) {
    		ship._y += ship_speed;
    	}
    }
    function ShipBoundary(){
    	if(ship._x <= 0){
    		ship._x += ship_speed;
    	}
    	if(ship._y <= 60){
    		ship._y += ship_speed;
    	}
    	if(ship._x >= Stage.width){
    		ship._x -= ship_speed;
    	}
    	if(ship._y >= Stage.height){
    		ship._y -= ship_speed;
    	}
    }
    
    function onMouseDown(){
    	// Create bullet
         d = _root.getNextHighestDepth();
         nb = createEmptyMovieClip( "bullet" + d, d);
    	// Position the bullet
         nb._x = ship._x;
         nb._y = ship._y;
         var dx:Number = _xmouse - ship._x;
         var dy:Number = _ymouse - ship._y;
         rads = Math.atan2( dy, dx);
    	// Give it speed from the last radian calculation
         nb.xs = Math.cos( rads) * bullet_speed;
         nb.ys = Math.sin( rads) * bullet_speed;
    	// Draw bullet graphic
         nb.lineStyle( 2, 0xFF0000);
         nb.lineTo( 5, 0);
    	// Rotate bullet
         nb._rotation = ship._rotation;
      
         bullets.push( nb);
    	 var shoot_sound = new Sound(); 
    	 shoot_sound.attachSound("shoot"); 
    	 shoot_sound.start(); 
    }
    function faceMouse(obj:MovieClip){
         	var dx:Number = _xmouse - obj._x;
         	var dy:Number = _ymouse - obj._y;
         	var rads:Number = Math.atan2( dy, dx);
         	obj._rotation = rads * (180 / Math.PI);
    	}
    	
    function moveToMouse(){
         var dx:Number = _xmouse - obj._x;
         var dy:Number = _ymouse - obj._y;
         rads = Math.atan2( dy, dx);
         obj._x += Math.cos( rads) * distance;
         obj._y += Math.sin( rads) * distance;
    	}
    function LoseLife(){
    	 var GameOver = new Sound(); 
    	 GameOver.attachSound("GameOver"); 
    	 GameOver.start(0, 1); 
    		life--;
    	 if (life == 0 ){
    		nextFrame();
    		}
    }
    
    function Enemy1(){	
    	for(i=0;i<2;i++){
    		attachMovie("happy", "happy"+i, i+0);
    		_root["happy"+i]._x += 100+i*xspace;
    		_root["happy"+i]._y += 100+yspace;
    		
    	if(_root["happy"+i].hitTest(cb)){ 
    	_root["happy"+i].swapDepths(_root.getNextHighestDepth());
    		_root["happy"+i].removeMovieClip();
    		score += 50;
    	}
    	} 
    }
    
    function ShootBullets(){
         for (i=0; i < bullets.length; i++){
              cb = bullets[i];
              cb._x += cb.xs;
              cb._y += cb.ys;
    	 }
    }
    	startDrag(pointer, 1);
    	Mouse.hide();

  19. #19
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    You are attaching enemies within enterFrame event. Even when its killed and removed, you again put it back next frame. The code for attaching enemy should be run only once when the game starts and only the part where you check collision with bullets should be run every frame.

  20. #20
    Flash Newbie
    Join Date
    Mar 2009
    Location
    UK
    Posts
    32
    Quote Originally Posted by tonypa View Post
    You are attaching enemies within enterFrame event. Even when its killed and removed, you again put it back next frame. The code for attaching enemy should be run only once when the game starts and only the part where you check collision with bullets should be run every frame.
    You mean that I shouldn't have the for loop? I am trying to 're-load' the enemy once it has been hit but in a different position but just now it shows 2 enemies on the screen at the same time.

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