0

所以,基本上我在使用 hitTesting 时遇到了这个问题。在我完成的所有 AS3 中,我从来没有遇到过这个错误。我正在使用拖放,基本上你必须将垃圾拖到垃圾箱中。非常直截了当。这是搞砸的代码,给了我错误:

    if(coin3.hitTestObject(wallet1)) {
coins +=1;
coin3.x -=7000;
}

所以这个错误真的很烦我,我需要修复它,它正在破坏我的游戏!我没有将对象分配给变量或数据类型,就这么简单。我该如何解决这个问题?我不知道什么是空!这是整个代码:

import flash.events.Event;
import flash.events.MouseEvent;
var coins:Number = 0;
var maxcoins:Number = 3;
coin2.addEventListener(MouseEvent.MOUSE_DOWN, coin2drag);
function coin2drag(e:MouseEvent)
{
    coin2.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, coin2undrag);
function coin2undrag(e:MouseEvent)
{
    coin2.stopDrag();
}

coin3.addEventListener(MouseEvent.MOUSE_DOWN, coin3drag);
function coin3drag(e:MouseEvent)
{
    coin3.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, coin3undrag);
function coin3undrag(e:MouseEvent)
{
    coin3.stopDrag();
}
coin4.addEventListener(MouseEvent.MOUSE_DOWN, coin4drag);
function coin4drag(e:MouseEvent)
{
    coin4.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, coin3undrag);
function coin4undrag(e:MouseEvent)
{
    coin4.stopDrag();
}

coin2.addEventListener(Event.ENTER_FRAME, coin2hit);
function coin2hit(e:Event)
{
    if (coin2.hitTestObject(wallet1))
    {
        coins +=  1;
        coin2.x +=  700000;
    }
    if (coins == maxcoins)
    {
        gotoAndStop(54);
    }
}


coin3.addEventListener(Event.ENTER_FRAME, coin3hit);
function coin3hit(e:Event)
{
    if (coin3.hitTestObject(wallet1))
    {

        coins +=  1;
        coin3.x +=  700000;
    }
};


coin4.addEventListener(Event.ENTER_FRAME, coin4hit);
function coin4hit(e:Event)
{
    if (coin4.hitTestObject(wallet1))

    {
        coins +=  1;
        coin4.x +=  700000;
    }
};
4

3 回答 3

0

This might get you started... Read about how Arrays work to better understand how to count and manage objects. This may or may not be ideal for whatever you're actually trying to do, but you can do some pretty nifty things with them... Also you mis-referenced one of your drag functions. I cleaned it up a bit to make it a little easier to manage.

import flash.events.Event;
import flash.events.MouseEvent;

var coins:Array();
//this creates an empty array

var maxcoins:int= 3;

coin2.addEventListener(MouseEvent.MOUSE_DOWN, drag);
coin2.addEventListener(MouseEvent.MOUSE_UP, undrag);
coin3.addEventListener(MouseEvent.MOUSE_DOWN, drag);
coin3.addEventListener(MouseEvent.MOUSE_UP, undrag);
coin4.addEventListener(MouseEvent.MOUSE_DOWN, drag);
coin4.addEventListener(MouseEvent.MOUSE_UP, undrag);

// if only dragging one object at a time, then you can just put all your drags into two     functions

function drag(e:MouseEvent)
{
coin2.startDrag();
coin3.startDrag();
coin4.startDrag();
}

function undrag(e:MouseEvent)
{
coin2.stopDrag();
coin3.stopDrag();
coin4.stopDrag();
}

/////////////////////////////////////////////////////////////////

coin2.addEventListener(Event.ENTER_FRAME, coin2hit);
function coin2hit(e:Event)
{
if (coin2.hitTestObject(wallet1))
{
    coins.push(coin2);
//this pushes the coin into the Array which will continue count until it is     cleared below ////

coin2.visible=false;
//setting coin2 to not be visible is simpler than moving it away, especially if the object is never being added again on stage. 
}

if (coins == maxcoins) // mind you this conditional will ONLY execute if coin2hit() is run.
{
    gotoAndStop(54);

    coins.pop(); //this clears the array ////
}
}


coin3.addEventListener(Event.ENTER_FRAME, coin3hit);
function coin3hit(e:Event)
{
if (coin3.hitTestObject(wallet1))
{

    coins.push(coin3);
    coin3.visible=false;
}
};


coin4.addEventListener(Event.ENTER_FRAME, coin4hit);
function coin4hit(e:Event)
{
if (coin4.hitTestObject(wallet1))

{
    coins.push(coin4);
    coin4.visible=false;
}
};
于 2012-10-10T21:58:19.353 回答
0

如果您使用代码严格实例化您的对象,请确保您正确分类您的对象,并相应地命名您的变量......即:

var coin3:Coin = new Coin;

其中“coin3”是“Coin”类的一个实例...根据您打算对该对象执行的操作,您可能需要:

addChild(coin3);
//this may sit inside a function, or directly after the var is created, depending on what you're trying to do

否则,如果您正在调用已经物理放置在舞台上的对象,请确保它具有您所调用的任何内容的实例名称。这是在属性窗口窗格中设置的。简单地将 MC 命名为“coin3”(或其他)是行不通的。

虽然没有看到更多代码,但很难知道发生了什么。如果类结构的概念是新概念,那么您绝对应该阅读它是如何工作的,因为它对于使用 AS3 等面向对象的语言正确创建和管理对象至关重要。

于 2012-10-10T21:20:31.037 回答
0

此错误意味着您尝试访问的对象不存在。在这种情况下,这意味着 coin3、wallet1 或 coin 变量在您尝试使用之前尚未正确创建(初始化)。

只需确保 coin3 和 wallet1 是您的电影剪辑的真实名称并且它们确实在舞台上,并确保在使用之前至少将硬币设置为任何值(如硬币 = 0;);。

于 2012-10-10T21:04:05.363 回答