删除一个对象时,它击中另一个对象AS3 [英] Removing an Object when it hits another object AS3

查看:169
本文介绍了删除一个对象时,它击中另一个对象AS3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里初学者。我在时间线上有一个实例名称为island的符号,所以基本上我想删除命中island的单元格。

  if(cell.hitTestObject(island)){

if(stage.contains(cell))
removeChild(cell);





我在moveCell函数下试过这个,但是它只删除一个单元格,而不是每一个击中岛屿的细胞。感谢大家!

这是我的代码到目前为止:

$ p $ package {
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;

public class Main扩展MovieClip {

public var cell:Cell;
public var group:Array;
public var gameTimer:Timer;


public function Main(){

cell = new Cell(400,-15);
addChild(cell);

group = new Array();
var newCell = new Cell(100,-15);
group.push(newCell);
addChild(newCell);


gameTimer =新的定时器(25);
gameTimer.addEventListener(TimerEvent.TIMER,moveCell);
gameTimer.start();


public function moveCell(timerEvent:TimerEvent):void {
$ b $ if(Math.random()<0.01){
var randomX :Number = Math.random()* 700;
var newCell:Cell = new Cell(randomX,-15);
group.push(newCell);
addChild(newCell);



每个(var i:MovieClip组){
if(i.hitTestObject(island)){

i.visible = false;
//i.parent.removeChild(i);

var score:int = 0;

得分++;

scoreOutPut.text = score.toString();



}
}



}

}


$ / code>


解决方案

因为您从 DisplayObjectContainer 中删除​​了 Cell 对象,所以无法访问空对象引用的属性或方法。 (它的父类),而不是来自 group 数组,所以在你的 for 循环的下一次迭代中,那个对象didn将不再存在,错误将被解雇。



为避免这种情况,您可以这样做:

  for var i:int = 0; i< group.length; i ++)
{
var cell:Cell = Cell(group [i]);
if(cell.hitTestObject(island))
{
cell.parent.removeChild(cell);
group.splice(i,1);
得分++;

$ b $ / code $ / pre
$ b $ code>,它应该是所有类的全局属性,每次都得到更新。



另外,为了使代码更加组织化和清晰,比如,为了创建单元格,你可以使用 createCell()方法:

  // 0是__x的默认值,-15是__y 
的默认值private function createCell(__ x:Number = 0,__y:Number = -15):void
{
var cell:Cell = new Cell(__ x,__y);
group.push(cell);
addChild(cell);





$ b然后你就可以在你的代码中的任何地方使用它,例如for你在构造函数中创建的两个第一个单元格:
$ b $ pre $ $ $ c $ public function Main()
{
/ / ..

createCell(400);
createCell(100);

// ...
}

如果(Math.random()< code> ; 0.01)
{
var randomX:Number = Math.random()* 700;
createCell(randomX);





$ b

另外,如果你真的不需要一个属性或方法

...



希望能有所帮助。


Beginner here. I have a symbol on the timeline with an instance name of 'island', so basically I want to remove the cells that hits the 'island'

if (cell.hitTestObject (island)) {

             if(stage.contains(cell))
             removeChild (cell);
         }

I tried this one under the moveCell function but it only removes one cell instead of every cell that hits the island. Thanks everyone!

Here's my code so far:

 package  {
    import flash.display.MovieClip;
    import flash.utils.Timer;
    import flash.events.TimerEvent;

    public class Main extends MovieClip {

        public var cell:Cell;
        public var group:Array;
        public var gameTimer:Timer;


        public function Main() {

            cell = new Cell (400, -15);
            addChild (cell);

            group = new Array();
            var newCell = new Cell (100, -15);
            group.push ( newCell);
            addChild(newCell);


            gameTimer = new Timer (25);
            gameTimer.addEventListener(TimerEvent.TIMER,moveCell);
            gameTimer.start();
        }

        public function moveCell (timerEvent:TimerEvent):void {

             if (Math.random() < 0.01) {
             var randomX:Number = Math.random() * 700;
             var newCell:Cell = new Cell (randomX, -15);
             group.push (newCell);
             addChild(newCell);
             }


         for each(var i:MovieClip in group) {
             if (i.hitTestObject(island)) {

                i.visible = false;
                //i.parent.removeChild(i); 

                var score:int = 0;

                 score ++;

                scoreOutPut.text = score.toString();



             }   
         }



        }

    }

}`

解决方案

You got the "Cannot access a property or method of a null object reference" because you've removed the Cell object from the DisplayObjectContainer (its parent) but not from the group array, so in the next iteration of your for loop, that object didn't exist anymore and that error will be fired.

To avoid that you can do like this :

for(var i:int = 0; i < group.length; i++)
{               
    var cell:Cell = Cell(group[i]);
    if (cell.hitTestObject(island))
    {
        cell.parent.removeChild(cell);
        group.splice(i, 1);     
        score++;
    }
}

For the score, it should be a global property for all your class to get updated every time.

Also, for your code to be more organised and clearer, it's better to put every task in a single method.

For example, for creating cells, you can use a createCell() method :

// 0 is the default value of __x and -15 is the default one of __y
private function createCell(__x:Number = 0, __y:Number = -15): void 
{
    var cell:Cell = new Cell(__x, __y);
    group.push(cell);
    addChild(cell);
}

Then you can use it in any place in your code, for example, for your two first cells that you create in the constructor :

public function Main()
{
    // ..

    createCell(400);
    createCell(100);

    // ...
}

Or inside the moveCell() method :

if (Math.random() < 0.01)
{
    var randomX:Number = Math.random() * 700;
    createCell(randomX);
}

Also, if you don't really need that a property or a method to be public, don't put it as public.

...

Hope that can help.

这篇关于删除一个对象时,它击中另一个对象AS3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆