Flash Collider不起作用

时间:2014-02-11 05:52:01

标签: actionscript-3 flash

我是Flash的新手,目前我正在尝试开发一款小游戏。但我的对手突然不再工作了。我不得不承认,我只是从我的教授那里得到了对撞机部分,我不知道它们是如何工作的。 当我插入位于舞台的整个背景的“BG”时,它工作得非常好。现在我想让我的角色与游戏中的每个平台发生冲突。 好吧,它以前工作过。突然间它有问题。为什么?! :/

Char_Collider_feet在我的Character_Control中 Platform_Collider位于BG MovieClip中,位于舞台......

错误消息:错误1009“空对象引用错误”。 我知道我的意思但是我找不到它的原因,特别是因为它在我的其他项目中工作...(好吧我不在那里使用文档类)

主要

package  
{
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.events.Event;
import flash.ui.Keyboard;



public class Main extends MovieClip 
{
public var char:Character_Control;
public var figure:MovieClip;
public var BG:MovieClip;
public var Platform:Platform_Collider;

var allKeys:Object = new Object();
var oCollider;


    public function Main() 
    {
        this.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
        this.stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
        this.stage.addEventListener(Event.ENTER_FRAME, update);
        this.stage.addEventListener(Event.ENTER_FRAME, Gravity);
        Start();
    }



    //Creates Chars and Objects
    public function Start()
    {
        char = new Character_Control(150,450);
        stage.addChild(char);
    }


    //Key pressed
    function keyPressed(e:KeyboardEvent)
    {
        allKeys[e.keyCode] = true;
    }

    //Key released
    function keyReleased(e:KeyboardEvent)
    {
        if(allKeys[e.keyCode] != null)
        allKeys[e.keyCode] = false
        char.stopMove();
    }


    //Sends keyCode to Character_Control
    function update(e:Event)
    {
        //Walk
        if(allKeys[Keyboard.LEFT])
        {char.Move("walk_left");}
        else if(allKeys[Keyboard.RIGHT])
        {char.Move("walk_right");}

        //Jump
        if(allKeys[Keyboard.SPACE])
        {char.Move("jump");}

        //Idle
        char.Move("idle");
    }


    //Tests Collision with Background
    function Gravity(_event:Event)
    {
            //Insert Object which hits "char"
            oCollider = char.hitBGCollider(BG);

            if (oCollider)
            {
                char.Fall(true);
                trace("I collide!");
            }
            else
            {
                char.Fall(false);
                trace("I fall!");
            }
    }


}
}

Character_Control

package
{

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


public class Character_Control extends MovieClip 
{
public var figure:MovieClip;

var speed:int = 5;
var currentSpeed:int = 0;
var jumpPower:int = 5;
var gravity:Number = 0.9;
var isJumping:Boolean = false;
var aCollider:Array = new Array();


    public function Character_Control(posx:Number,posy:Number) 
    {
        this.x = posx;
        this.y = posy;
        this.addEventListener("RegisterCharColliderFeet", RegisterCharColliderFeet); 
        this.addEventListener(Event.ENTER_FRAME, update);
    }

    //Controls Jumps
    public function update(e:Event)
    {
        if(figure.currentAnim=="jumpUP")
        {
            figure.y -=jumpPower;
        }
        else if(figure.currentAnim=="jumpDOWN")
        {
            figure.y +=jumpPower;
        }
    }


    //Moves Character, after Keyboard input received
    public function Move(_action:String)
    {
        switch(_action) 
        { 
            case "walk_left": 
            figure.scaleX = -1;
            currentSpeed = 5;
            figure.x -= currentSpeed*scaleX;
            if(figure.currentAnim=="nothing" || figure.currentAnim=="idle")
            {
                figure.setAnimation("walk");
                isJumping = false;
            }
            break; 

            case "walk_right": 
            figure.scaleX = 1;
            currentSpeed = 5;
            figure.x += currentSpeed*scaleX;
            if(figure.currentAnim=="nothing" || figure.currentAnim=="idle")
            {
                figure.setAnimation("walk");
                isJumping = false;
            }
            break; 

            case "jump": 
            if(!isJumping)
            {
                figure.setAnimation("jump");
                isJumping = true;
            }
            break; 

            case "idle":
            if(figure.currentAnim=="nothing")
            {
                figure.setAnimation("idle");
                isJumping = false;
            }
            break;

            default: 
            figure.setAnimation("idle"); 
            break; 
        }
    }

    //Stopps Move after Key Released
    public function stopMove()
    {
        if (figure.currentAnim=="walk")
        {figure.setAnimation("idle");}
    }




    //For Char_Collider_fett
    public function RegisterCharColliderFeet(_event:Event)
    {
        aCollider.push(_event.target);
        _event.target.visible = false; 
    }



    //Tests Collision with the Object received 
    public function hitBGCollider(_oTest:MovieClip):Char_Collider_feet 
    {
        var posTest:Point = new Point();

        for each (var oCollider:Char_Collider_feet in aCollider)
        {
            posTest.x = oCollider.x;
            posTest.y = oCollider.y;
            posTest = figure.localToGlobal(posTest);
            if (_oTest.hitTestPoint(posTest.x,posTest.y,true))
            {
                return oCollider;
            }
        }
        return null;
    }


    //Falls every Frame until Collision
    public function Fall(collision:Boolean)
    {
        if (collision)
        {
            gravity *= 1.1;
            figure.y +=2*gravity;
        }
    }

}
}

1 个答案:

答案 0 :(得分:0)

我不知道你正在使用什么样的引擎,但看起来问题就在这里

public var BG:MovieClip;
public var Platform:Platform_Collider;

我认为应该是这样的?

public var BG:Platform_Collider;

所以这看起来现在可以正常工作

char.hitBGCollider(BG);