我坚持在对象类文件中创建一个命中检测功能,所以如果它与舞台上的玩家发生碰撞就会检测到它。但是我从文档类文件调用的公共变量得到“错误:1067访问未定义的属性”。 这是我想要调用公共变量的对象类中的函数:
public function hitTesting(obj:Object) {
if (hitTestPoint(obj.x + leftBumpPoint.x, obj.y + pubVarsleftBumpPoint.y, true)) {
trace("leftCollide");
leftCollide = true;
} else {
leftCollide = false;
}
if (hitTestPoint(obj.x + rightBumpPoint.x, obj.y + rightBumpPoint.y, true)) {
trace("right hit");
rightCollide = true;
} else {
rightCollide = false;
}
if (hitTestPoint(obj.x + upBumpPoint.x, obj.y + upBumpPoint.y, true)) {
trace("up hit");
upCollide = true;
} else {
upCollide = false;
}
}
编辑: 文档类代码:
package {
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.*;
import flash.ui.Keyboard;
import flash.display.*;
import flash.utils.Timer;
import flash.sampler.NewObjectSample;
import flash.geom.Point;
import MediumEnemy;
public class PublicVariables extends MovieClip {
public var upPressed: Boolean = false;
public var downPressed: Boolean = false;
public var leftPressed: Boolean = false;
public var rightPressed: Boolean = false;
public var xSpeed: Number = 0;
public var ySpeed: Number = 0;
public var gravity: Number = 1;
public var scrollX: Number = 0;
public var scrollY: Number = 0;
public var speedConstant: int = 8;
public var friction: Number = 0.55;
public var leftCollide: Boolean = false;
public var rightCollide: Boolean = false;
public var upCollide: Boolean = false;
public var downCollide: Boolean = false;
public var leftBumpPoint: Point = new Point(-30, -55);
public var rightBumpPoint: Point = new Point(30, -55);
public var upBumpPoint: Point = new Point(0, -120);
public var downBumpPoint: Point = new Point(0, 80);
public var jumpConstant: Number = -65;
public var gravityConstant: Number = 5;
public function PublicVariables() {
对象类的代码:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.geom.Point;
import PublicVariables;
public class MediumEnemy extends PublicVariables {
//private var xSpeed: Number = 8 //randomValue(3, 6); //not being used
//private var ySpeed: Number = 8 //randomValue(8, 15); not being used
private var direction: Boolean = false;
public function MediumEnemy(xLocation: Number, yLocation: Number) {
x = xLocation + randomValue(0, 550);
y = yLocation; //set x position to random value
if (x < 275) //determine whether to make the direction of motion left or right
{
direction = true
//trace("go left")
} else {
direction = false;
//trace("go right");
}
this.addEventListener(Event.ENTER_FRAME, animate); //add this
}
function animate(event: Event) { //looping code
switch (direction) {
case false:
{
x -= xSpeed;
break;
}
case true: //go right
{
x += xSpeed;
break;
}
}
y += ySpeed;
}
public function hitTesting(obj: Object) {
if (hitTestPoint(obj.x + leftBumpPoint.x, obj.y + leftBumpPoint.y, true)) {
trace("leftCollide");
leftCollide = true;
} else {
leftCollide = false;
}
if (hitTestPoint(obj.x + rightBumpPoint.x, obj.y + rightBumpPoint.y, true)) {
trace("right hit");
rightCollide = true;
} else {
rightCollide = false;
}
if (hitTestPoint(obj.x + upBumpPoint.x, obj.y + upBumpPoint.y, true)) {
trace("up hit");
upCollide = true;
} else {
upCollide = false;
}
}
}
}
仅供参考:我打算以某种方式将'player'对象传递给hitTesting函数以使用参数获取其x位置。
答案 0 :(得分:1)
建议您采用一种好的方法来分析代码并了解可能导致问题的原因等等。
试试这个:
public class MediumEnemy extends MovieClip {
private var direction:Boolean = false;
private var publicVariables:PublicVariables = new PublicVariables();
public function MediumEnemy(xLocation:Number, yLocation:Number) {
x = xLocation + randomValue(0, 550);
y = yLocation; // set x position to random value
if (x < 275) // determine whether to make the direction of motion left or right
{
direction = true;
// trace("go left")
} else {
direction = false;
// trace("go right");
}
this.addEventListener(Event.ENTER_FRAME, animate); // add this
}
private function animate(event:Event) { // looping code
switch (direction) {
case false: {
x -= publicVariables.xSpeed;
break;
}
case true: // go right
{
x += publicVariables.xSpeed;
break;
}
}
y += publicVariables.ySpeed;
}
public function hitTesting(obj:Object) {
if (hitTestPoint(obj.x + publicVariables.leftBumpPoint.x, obj.y + publicVariables.leftBumpPoint.y, true)) {
trace("leftCollide");
publicVariables.leftCollide = true;
} else {
publicVariables.leftCollide = false;
}
if (hitTestPoint(obj.x + publicVariables.rightBumpPoint.x, obj.y + publicVariables.rightBumpPoint.y, true)) {
trace("right hit");
publicVariables.rightCollide = true;
} else {
publicVariables.rightCollide = false;
}
if (hitTestPoint(obj.x + publicVariables.upBumpPoint.x, obj.y + publicVariables.upBumpPoint.y, true)) {
trace("up hit");
publicVariables.upCollide = true;
} else {
publicVariables.upCollide = false;
}
}
}