Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 错误时具有相同功能的不同对象-Actionscipt_Actionscript 3 - Fatal编程技术网

Actionscript 3 错误时具有相同功能的不同对象-Actionscipt

Actionscript 3 错误时具有相同功能的不同对象-Actionscipt,actionscript-3,Actionscript 3,我正在尝试创建一个拖放游戏 我使用了一个数组,它可以使用拖放功能,但不能使用IF函数 相反,它向我显示了以下错误: “TypeError:Error#1010:术语未定义且没有属性。 在 Test_game_fla::main timeline/drop()[Test_game_fla.main timeline::frame1:38]“ 这是我的密码: var s = 0; score.text = s; var mixed:Array = new Array; mix

我正在尝试创建一个拖放游戏

我使用了一个数组,它可以使用拖放功能,但不能使用
IF
函数

相反,它向我显示了以下错误:

“TypeError:Error#1010:术语未定义且没有属性。 在 Test_game_fla::main timeline/drop()[Test_game_fla.main timeline::frame1:38]“

这是我的密码:

var s = 0;          
score.text = s;

var mixed:Array = new Array;

mixed.push(orange);
mixed.push(cheese);
mixed.push(lobbio);
mixed.push(meat);
mixed.push(fish);

for (var i:uint = 0; i < mixed.length; i++) {

mixed[i].addEventListener(MouseEvent.MOUSE_DOWN, drag);
mixed[i].addEventListener(MouseEvent.MOUSE_UP, drop);


function drag(e)
{
e.target.startDrag();
}

function drop(e)
{
e.target.stopDrag();

if( (cheese.y > 50 && cheese.y < 150) && (cheese.x > 480 && cheese.x < 570) )
{
    cheese.x = -50;
    cheese.y = -50;
    s = s + 10;
    score.text = s;
}

if( (mixed[i].y > 50 && mixed[i].y < 150) && (mixed[i].x > 480 && mixed[i].x 
< 570) )
{
    mixed[i].y = -50;
    mixed[i].x = -50;
    s = s + 10;
    score.text = s;
}


}

}
var s=0;
score.text=s;
var mixed:数组=新数组;
混合。推(橙色);
混合。推(奶酪);
混合推(lobbio);
混合。推(肉);
混合。推(鱼);
对于(变量i:uint=0;i50和奶酪y<150)和((奶酪x>480和奶酪x<570))
{
奶酪.x=-50;
奶酪y=-50;
s=s+10;
score.text=s;
}
如果((混合[i].y>50&&mixed[i].y<150)&&mixed[i].x>480&&mixed[i].x
< 570) )
{
混合[i].y=-50;
混合[i].x=-50;
s=s+10;
score.text=s;
}
}
}

U可以这样修复它

var s = 0;          
score.text = s;

var mixed:Array = new Array;

mixed.push(orange);
mixed.push(cheese);
mixed.push(lobbio);
mixed.push(meat);
mixed.push(fish);

for (var i:uint = 0; i < mixed.length; i++) {
    mixed[i].addEventListener(MouseEvent.MOUSE_DOWN, drag);
    mixed[i].addEventListener(MouseEvent.MOUSE_UP, drop);
}


function drag(e)
{
    e.currentTarget.startDrag();
}

function drop(e)
{
    var mix:Sprite = Sprite(e.currentTarget);
    mix.stopDrag();


    if( (cheese.y > 50 && cheese.y < 150) && (cheese.x > 480 && cheese.x < 570) )
    {
        cheese.x = -50;
        cheese.y = -50;
        s = s + 10;
        score.text = s;
    }
    //Error in mixed[i] ; at last i = mixed.length mixed[i] is null. 
    //Event has a property "currentTarget" this can get u select target
    if( (mix.y > 50 && mix.y < 150) && mix.x > 480 && mix.x < 570) )
    {
        mix.y = -50;
        mix.x = -50;
        s = s + 10;
        score.text = s;
    }

}
var s=0;
score.text=s;
var mixed:数组=新数组;
混合。推(橙色);
混合。推(奶酪);
混合推(lobbio);
混合。推(肉);
混合。推(鱼);
对于(变量i:uint=0;i50和奶酪y<150)和((奶酪x>480和奶酪x<570))
{
奶酪.x=-50;
奶酪y=-50;
s=s+10;
score.text=s;
}
//mixed[i]中出错;最后i=mixed.length mixed[i]为空。
//事件有一个属性“currentTarget”,它可以获取u选择目标
如果((mix.y>50和&mix.y<150)和&mix.x>480和&mix.x<570))
{
mix.y=-50;
x=-50;
s=s+10;
score.text=s;
}
}

U可以这样修复它

var s = 0;          
score.text = s;

var mixed:Array = new Array;

mixed.push(orange);
mixed.push(cheese);
mixed.push(lobbio);
mixed.push(meat);
mixed.push(fish);

for (var i:uint = 0; i < mixed.length; i++) {
    mixed[i].addEventListener(MouseEvent.MOUSE_DOWN, drag);
    mixed[i].addEventListener(MouseEvent.MOUSE_UP, drop);
}


function drag(e)
{
    e.currentTarget.startDrag();
}

function drop(e)
{
    var mix:Sprite = Sprite(e.currentTarget);
    mix.stopDrag();


    if( (cheese.y > 50 && cheese.y < 150) && (cheese.x > 480 && cheese.x < 570) )
    {
        cheese.x = -50;
        cheese.y = -50;
        s = s + 10;
        score.text = s;
    }
    //Error in mixed[i] ; at last i = mixed.length mixed[i] is null. 
    //Event has a property "currentTarget" this can get u select target
    if( (mix.y > 50 && mix.y < 150) && mix.x > 480 && mix.x < 570) )
    {
        mix.y = -50;
        mix.x = -50;
        s = s + 10;
        score.text = s;
    }

}
var s=0;
score.text=s;
var mixed:数组=新数组;
混合。推(橙色);
混合。推(奶酪);
混合推(lobbio);
混合。推(肉);
混合。推(鱼);
对于(变量i:uint=0;i50和奶酪y<150)和((奶酪x>480和奶酪x<570))
{
奶酪.x=-50;
奶酪y=-50;
s=s+10;
score.text=s;
}
//mixed[i]中出错;最后i=mixed.length mixed[i]为空。
//事件有一个属性“currentTarget”,它可以获取u选择目标
如果((mix.y>50和&mix.y<150)和&mix.x>480和&mix.x<570))
{
mix.y=-50;
x=-50;
s=s+10;
score.text=s;
}
}
一些需要解决的问题:

(1)为变量指定数据类型。表示
var s=0
表明
s
是一个数值变量,对吗?Well
score.text
需要字符串而不是数字,因此必须使用强制转换来转换。尝试:

var s : int  = 0; //define a numerical variable
score.text = String(s); //cast number into String type (for usage as text)
(2)不要将函数放入For循环中!!!甚至不要将函数放在其他函数中(除非您知道匿名函数是什么,并且您可以证明需要它)

(3)您可以通过以下方式缩短某些代码键入:

  • 递增?使用
    s+=10
    避免更长的
    s=s+10。也可以是
    -=
    /=
  • 如果将相同的值设置为多个变量,则如下所示:
    cheese.x=cheese.y=-50
(4)不要试图通过
mixed.length
访问
mixed[i]
。由于长度在某一点上为5,编译器会看到类似以下指令:
mixed[5]。addEventListener…

但是第一个项目的数组从零开始,因此您应该知道第五个项目实际上在
混合[4]。addEventListener…

另一个
mixed[5]。某物
(与:
mixed[mixed.length]相同)。某物
)不存在

PS:我本以为设置
I
可以防止超过数组大小,因为如果
I
必须小于
。length
I==5
永远不会发生


无论如何。。。下面的代码未经测试(此处没有AS3编译器),但请尝试以下操作:

var s : int = 0;          
score.text = String(s);

var mixed:Array = new Array;

mixed.push(orange); mixed.push(cheese);
mixed.push(lobbio); mixed.push(meat); mixed.push(fish);

for (var i:uint = 0; i <= (mixed.length-1); i++) 
{
    mixed[i].addEventListener(MouseEvent.MOUSE_DOWN, drag);
    mixed[i].addEventListener(MouseEvent.MOUSE_UP, drop);

} //end "For" loop

function drag(e) : void { e.target.startDrag(); }

function drop(e) : void
{
    e.target.stopDrag();

    if( (cheese.y > 50 && cheese.y < 150) && (cheese.x > 480 && cheese.x < 570) )
    {
        cheese.x = cheese.y = -50;
        s += 10; //# achieves same thing as... s = s + 10;
        score.text = String(s);
    }

    if( (Sprite(e.currentTarget).y > 50 && Sprite(e.currentTarget).y < 150) && (Sprite(e.currentTarget).x > 480 && Sprite(e.currentTarget).x < 570) )
    {
        Sprite(e.currentTarget).x = Sprite(e.currentTarget).y = -50;
        s += 10;
        score.text = String(s);
    }
} //end Function "drop"
var s:int=0;
score.text=字符串;
var mixed:数组=新数组;
混合。推(橙色);混合。推(奶酪);
混合推(lobbio);混合。推(肉);混合。推(鱼);
对于(变量i:uint=0;i 50&&cheese.y<150)和(cheese.x>480&&cheese.x<570))
{
奶酪.x=奶酪.y=-50;
s+=10;//#实现了与……s=s+10相同的效果;
score.text=字符串;
}
if((精灵(e.currentTarget).y>50&&Sprite(e.currentTarget).y<150)&&Sprite(e.currentTarget).x>480&&Sprite(e.currentTarget).x<570))
{
精灵(e.currentTarget).x=精灵(e.currentTarget).y=-50;
s+=10;
score.text=字符串;
}
}//结束函数“drop”
一些需要解决的问题:

(1)为变量指定数据类型。表示
var s=0
表明
s
是一个数值变量,对吗?Well
score.text
需要字符串而不是数字,因此必须使用强制转换来转换。尝试:

var s : int  = 0; //define a numerical variable
score.text = String(s); //cast number into String type (for usage as text)
(2)不要将函数放入For循环中!!!不要把函数放在其他函数中