TypeError:错误#1006:值不是函数 - 我做错了什么?

时间:2014-04-23 15:05:15

标签: actionscript-3 compiler-errors

首先,我通过我的搜索意识到这已被问过一千次了,很抱歉,但我在解释答案时遇到了麻烦。我是ActionScript 3的新手,也是一般的新手程序员。

所以我编写了一个简单的游戏,其中一些球移动并加速。但是,每当我调用碰撞检查函数(isWithin)时,它都会给出错误#1006:值不是函数。我是否正确地打电话或宣布该功能?

import flash.ui.Keyboard;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.text.TextField;
import flash.text.TextFormat;

// add the event listeners
PlayerBall.addEventListener(Event.ENTER_FRAME, update);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);

// this array holds references to all the keys

var keys:Array = [];

var xSpeed1:Number = 0;
var ySpeed1:Number = 0;

var xSpeed2:Number = 0;
var ySpeed2:Number = 0;

var hasFlag:int = 0;

var collide:Boolean = false;

function isWithin(object1:Object, object2:Object, barrier:Number):Boolean
{
var distance:Number = Math.sqrt((object1.x - object2.x)(object1.x - object2.x) + (object1.y - object2.y)(object1.y - object2.y));

if(distance <= barrier)
{
    return(true);
}
else
{
    return(false);
}
}

collide = isWithin(PlayerBall, PlayerBall2, PlayerBall.width);
trace(collide);

function update(e:Event):void
{

// GETTING KEYBOARD INPUT FOR FRAME
if(keys[Keyboard.UP])
{
    ySpeed1 = ySpeed1 < -5 ? ySpeed1 : ySpeed1 - .3;
}
if(keys[Keyboard.DOWN])
{
    ySpeed1 = ySpeed1 > 5 ? ySpeed1 : ySpeed1 + .3;
}
if(!(keys[Keyboard.UP]) && !(keys[Keyboard.DOWN]))
{
    ySpeed1 = ySpeed1 > 0 ? ySpeed1 - .2 : ySpeed1 + .2;
}


if(keys[Keyboard.RIGHT])
{
    xSpeed1 = xSpeed1 > 5 ? xSpeed1 : xSpeed1 + .3;
}
if(keys[Keyboard.LEFT])
{
    xSpeed1 = xSpeed1 < -5 ? xSpeed1 : xSpeed1 - .3;
}
if(!(keys[Keyboard.RIGHT]) && !(keys[Keyboard.LEFT]))
{
    xSpeed1 = xSpeed1 > 0 ? xSpeed1 - .2 : xSpeed1 + .2;
}

if(keys[Keyboard.W])
{
    ySpeed2 = ySpeed2 < -5 ? ySpeed2 : ySpeed2 - .3;
}
if(keys[Keyboard.S])
{
    ySpeed2 = ySpeed2 > 5 ? ySpeed2 : ySpeed2 + .3;
}
if(!(keys[Keyboard.W]) && !(keys[Keyboard.S]))
{
    ySpeed2 = ySpeed2 > 0 ? ySpeed2 - .2 : ySpeed2 + .2;
}


if(keys[Keyboard.D])
{
    xSpeed2 = xSpeed2 > 5 ? xSpeed2 : xSpeed2 + .3;
}
if(keys[Keyboard.A])
{
    xSpeed2 = xSpeed2 < -5 ? xSpeed2 : xSpeed2 - .3;
}
if(!(keys[Keyboard.A]) && !(keys[Keyboard.D]))
{
    xSpeed2 = xSpeed2 > 0 ? xSpeed2 - .2 : xSpeed2 + .2;
}

// MOVEMENT

PlayerBall.x += xSpeed1;
PlayerBall.y += ySpeed1;

PlayerBall2.x += xSpeed2;
PlayerBall2.y += ySpeed2;

if(hasFlag == 1)
{
    flag.x = PlayerBall.x;
    flag.y = PlayerBall.y;
}
if(hasFlag == 2)
{
    flag.x = PlayerBall2.x;
    flag.y = PlayerBall2.y
}

// COLLISION CHECKING

collide = isWithin(PlayerBall, flag, PlayerBall.width/2 + flag.width/2);

if(flag.visible && collide)
{
    hasFlag = 1;
    flag.visible = false;

    flag.x = PlayerBall.x;
    flag.y = PlayerBall.y;
}

collide = isWithin(PlayerBall2, flag, (PlayerBall2.width/2 + flag.width/2));

if(flag.visible && collide)
{
    hasFlag = 2;
    flag.visible = false;

    flag.x = PlayerBall2.x;
    flag.y = PlayerBall2.y;
}
}

function onKeyDown(e:KeyboardEvent):void
{
    keys[e.keyCode] = true;
}

function onKeyUp(e:KeyboardEvent):void
{
keys[e.keyCode] = false;
}

2 个答案:

答案 0 :(得分:0)

您缺少乘法运算符。请改变行

var distance:Number = Math.sqrt((object1.x - object2.x)(object1.x - object2.x) + (object1.y - object2.y)(object1.y - object2.y));

var distance:Number = Math.sqrt((object1.x - object2.x)*(object1.x - object2.x) + (object1.y - object2.y)*(object1.y - object2.y));

答案 1 :(得分:0)

好吧,我可以看到你的问题&#39; var distance&#39;计算。如果你的意思是乘以括号中的减法值,你应该加上一个&#39; *&#39;在每对的结束和开始括号之间,即:

var distance:Number = Math.sqrt((object1.x - object2.x)*(object1.x - object2.x) + (object1.y - object2.y)*(object1.y - object2.y));

我猜错了,因为编译器正在将每对中的第二组括号解释为包含函数的参数。因此,价值不是一种功能&#39;。