AS3 - how to remove a child when it reaches a certain point, and re add the child

StackOverflow https://stackoverflow.com/questions/23565793

  •  18-07-2023
  •  | 
  •  

質問

So first off i'd like to state that i am not very good at AS3, i'm completely self taught so i am sure that there are many things i've done badly, inefficiently or plain wrong and i'm happy for any comments on these if there is things i can improve.

In addition, i have done this with classes, however i am now running into a time problem and have decided to make things work first and sort out the class files properly later, i know this is also very bad practice and makes more work than needed, however as i'm not so confident in their use, i would rather complete the work.

So to my question, i am creating a platform game and so far i've got the movement and jumping from platform to platform down, and i am trying to add the scrolling functionality of the game now, the way i am attempting this is by removing a child when it reaches the bottom of the screen, and then adding that child back in a random position.

on the start function i have this array to add the child and randomize it's position:

for(i=0;i<8;i++) { PlatformInstance[i] =new Platform(); PlatformInstance[i].y= Math.random()*900; PlatformInstance[i].x= Math.random() *1500; stage.addChild(PlatformInstance[i]); }

the code for my collisions and the scrolling is shown below:

for (i=0; i<8; i++) {
if (PlatformInstance[i].hitTestPoint (Smallclock_hero.x,Smallclock_hero.y+130,true)) {
yescollision = true;
 if (keyboard_input.is_up() && yescollision == true)
    {
    trace ("boop")
    Smallclock.y_speed = -40
    yescollision = false;
    }

else {
Smallclock.y_speed *= 0;
}
}
if (Scrolling == true) {
PlatformInstance[i].y += 1; // this moves the platforms down.
}
}

is there a simple and easy way to remove the PlatformInstance when it reaches a point say (1000) and then add the instance again, with the same randomization code?

thanks in advance.

Adding all of my class files for clarities sake, not sure if you will need them

Start.as

package com {

 import flash.display.MovieClip;
import flash.events.Event; 

public class Start extends MovieClip {   
public var keyboard_input:Keys;     
 public var Smallclock_hero = new Smallclock;
public var BasePlatformInstance:BasePlatform = new BasePlatform;
public var BackgroundInstance:Background = new Background();
public var PlatformInstance:Platform = new Platform();
public var keyboard_sprite = new MovieClip();
public static var yescollision:Boolean = false
var yScrollSpeed:int = 1;
var Scrolling:Boolean = false;
var i:int =0;
public var Running:Boolean = true;



 public function Start () {
 trace("Hello")
 addChild (BackgroundInstance);
addChild (Smallclock_hero);
addChild (BasePlatformInstance);
BasePlatformInstance.x = 0;
BasePlatformInstance.y = 980;
 BackgroundInstance.height = stage.stageHeight +50 ;
 BackgroundInstance.width = stage.stageWidth  +50 ;
 Smallclock_hero.init();
var keyboard_sprite = new MovieClip();
addChild (keyboard_sprite);
keyboard_input = new Keys (keyboard_sprite);
stage.addEventListener(Event.ENTER_FRAME,on_enter);
addEventListener (Event.ENTER_FRAME, collisions);
//addEventListener (Event.ENTER_FRAME,refreshPlatform)
for(i=0;i<8;i++) {
        PlatformInstance[i] =new Platform();
     PlatformInstance[i].y= Math.random()*900;
     PlatformInstance[i].x= Math.random() *1500;
     stage.addChild(PlatformInstance[i]);

 }


 }  

 public function on_enter(event:Event) {

if (keyboard_input.is_left()){
Smallclock_hero.apply_force(-1,0);
Smallclock_hero.scaleX = -1

}
if (keyboard_input.is_right()) {
Smallclock_hero.apply_force(1,0);
Smallclock_hero.scaleX = 1
}


  }

  public function collisions (e:Event) {
 if (BasePlatformInstance.hitTestPoint (Smallclock_hero.x,Smallclock_hero.y+130, true)) {
    //trace ("touching!")
    yescollision = true;

    if (keyboard_input.is_up()) 
    {

    Smallclock.y_speed = -40
    Start.yescollision = false;
    }
if (Smallclock.y_speed ==-40) {
    Scrolling = true;
    removeChild(BasePlatformInstance);
}


else {

Smallclock.y_speed *= 0;
}
}

 for (i=0; i<8; i++) {

 if (PlatformInstance[i].hitTestPoint (Smallclock_hero.x,Smallclock_hero.y+130,     true))      {
yescollision = true;
 if (keyboard_input.is_up() && yescollision == true)
    {
    trace ("boop")
    Smallclock.y_speed = -40
    yescollision = false;
    }

else {
     Smallclock.y_speed *= 0;
  }
 }
 if (Scrolling == true) {
PlatformInstance[i].y += 1;
 }
 }

Smallclock.as

package  com {

import flash.display.Sprite;
import flash.events.Event;
public class Smallclock extends Sprite {
    private var x_speed:Number;
    public static var y_speed:Number;
    private var power:Number;
    public  var friction:Number;
    public static var gravity:Number;
    public static var jumping:Boolean = false;
    public static var jumppwr:Number;
    public static var jumpSpeedLimit:int = 15;


    public function Smallclock() {
        addEventListener (Event.ENTER_FRAME, move);

    }
    private function move (e:Event) {
        x+=x_speed;
        y+=y_speed;
        y_speed += gravity
        x_speed *= friction  ;
        y_speed *= friction  ;
        if (x < -25) { 
            x = 1650

        }
        if (x > 1650) {
            x = -25
        }

    }
    public function apply_force (x_force,y_force){
        x_speed += (x_force*power);
        y_speed += (y_force*power);
    }
    public function init() {
        jumppwr = 2;
        gravity = 1.0;
        power = 0.8;
        friction = 0.9;
        x_speed = 0;
        y_speed = 0;
        x= stage.stageWidth/2;
        y = 850;


    }
}

}

Keys.as

package com {

import flash.events.KeyboardEvent;
import flash.ui.Keyboard;

public class Keys {
    private var press_left = false;
    private var press_right = false;
    private var press_up = false;
    private var press_down = false;
    private var press_space = false;

    public function Keys(movieclip) {
        movieclip.stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down);
        movieclip.stage.addEventListener(KeyboardEvent.KEY_UP, key_up);/**/
    }
    public function is_left() {
        return press_left;
    }
    public function is_right() {
        return press_right;
    }
    public function is_up() {
        return press_up;
    }
    public function is_down() {
        return press_down;
    }
    public function is_space() {
        return press_space;
    }
    public function key_down(event:KeyboardEvent) {
        if (event.keyCode == 32) {
            press_space = true;
        }
        if (event.keyCode == 37) {
            press_left = true;
        }
        if (event.keyCode == 38) {
            press_up = true;

        }
        if (event.keyCode == 39) {
            press_right = true;
        }
        if (event.keyCode == 40) {
            press_down = true;
        }
    }
    public function key_up(event:KeyboardEvent) {
        if (event.keyCode == 32) {
            press_space = false;
        }
        if (event.keyCode == 37) {
            press_left = false;
        }
        if (event.keyCode == 38) {
            press_up = false;
        }
        if (event.keyCode == 39) {
            press_right = false;
        }
        if (event.keyCode == 40) {
            press_down = false;
        }
    }
}
 }
役に立ちましたか?

解決

I am not going to read thru all your code, sorry.

Of course there is. In the interval/event handler where you are updating your positions, you have to loop through all your platforms and check whether their position is >= 1000. If it is, you don't need to remove it, just randomize it and set its position again with the code you already have:

for(i=0;i<8;i++) {
   if(PlatformInstance[i].x >= 1000) {
      var inst:Platform = PlatformInstance[i];
      inst.y= Math.random() * 900;
      inst.x= Math.random() * 1500;
}

That should work just fine. If you really need to remove it (??), you can do so with stage.removeChild(inst) and then stage.addChild(inst) again.

A few tips from that few code I read in your question: do not give instance/variable names with capitalized first letter. That should be a class name (PlatformInstance is obviously an instance of Array or Vector, use platformInstance). Do not add object directly to stage. If possible, add them to your stage owner (which is your document class if you write your code in classes).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top