我正在尝试创建一个小的交互式拖放程序,它将检查放置位置是否是 fishBowl 类对象,或者将拖动的对象放在 fishBowl 对象中,或者如果它没有击中 fishBowl,将跳回原始创建的位置目的。
我遇到的问题是 if 语句无法将放置位置识别为类的对象,并且经过两天的不同编码方式后,我无法让它工作。
这是我的 J_Objects 类,其中子类来自:
package classes {
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.MovieClip;
public class J_Objects extends Sprite {
private var xSpeed:int = 0;
private var ySpeed:int = 2;
private var topPadding:int = 250;
private var drag:Boolean = false;
private var tempSpeedX:int = xSpeed;
private var tempSpeedY:int = ySpeed;
public function J_Objects(_x:int, _y:int) {
// constructor code
this.x = _x;
this.y = _y;
this.buttonMode = true; //add hand cursor on mouse hover
this.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag);
this.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
}
public function move():void {
if (this.xSpeed <= -1) {
this.scaleX = -1;
}
else if (this.xSpeed >= 1) {
this.scaleX = 1;
}
//Check if at the left or right and change direction if so
if ((this.x - this.width/2 <= 0) || (this.x + this.width/2 >= stage.stageWidth)) {
this.xSpeed *= -1;
//this.scaleX *= -1;
}//Also check if that top or bottom and if so change direction
else if ((this.y - this.topPadding - this.height/2 <= 0) || (this.y + this.height/2 >= stage.stageHeight)) {
this.ySpeed *= -1;
}
this.x += this.xSpeed;
this.y += this.ySpeed;
}
/* Drag and Drop
Makes the specified symbol instance moveable with drag and drop.
*/
public function clickToDrag(evt:MouseEvent):void
{
this.startDrag();
pickedUp();
xSpeed = 0;
ySpeed = 0;
//var dragEvent:Event = new Event('DRAG_OBJECT', true );
//this.dispatchEvent(dragEvent);
}
public function releaseToDrop(evt:MouseEvent):void
{
this.stopDrag();
//fishBowl.object();
if (evt.currentTarget.hitTestObject(evt.target.name == "fishBowl")) {
trace("Fishbowl got hit");
}
else {
xSpeed = tempSpeedX;
ySpeed = tempSpeedY;
putDown();
trace("Dropped objects was: ", this.name);
}
/*if (evt.target.hitTestObject(evt.target.name) == fishBowl) {
trace("Fishbowl got hit");
}
else {
xSpeed = tempSpeedX;
ySpeed = tempSpeedY;
putDown();
trace("Dropped objects was: ", this.name);
}*/
}
public function pickedUp():void {
//var tempX:int = 0;
//var tempY:int = 0;
//tempX = this.x;
//tempY = this.y;
//this.x =
}
public function putDown():void {
//pickedUp();
//this.x = tempX;
//this.y = tempY;
}
}
}
我在 if 语句中尝试了几种不同的方法,但我得到了:无法将布尔值更改为类,无法访问静态变量等。
这些是子对象的代码:
package classes {
import flash.display.MovieClip;
public class J_GoldFish extends J_Objects {
var tempX:int = 0;
var tempY:int = 0;
public function J_GoldFish(_x:int, _y:int) {
// constructor code
super(_x, _y);
tempX = _x;
tempY = _y;
}
override public function putDown():void {
this.x = tempX;
this.y = tempY;
}
}
}
package classes {
import flash.display.MovieClip;
public class J_BlueFish extends J_Objects {
var tempX:int = 0;
var tempY:int = 0;
public function J_BlueFish(_x:int, _y:int) {
// constructor code
super(_x, _y);
tempX = _x;
tempY = _y;
}
override public function putDown():void {
this.x = tempX;
this.y = tempY;
}
}
}
package classes {
import flash.display.Sprite;
public class J_FishBowl extends Sprite {
public function J_FishBowl(_x:int, _y:int) {
// constructor code
this.x = _x;
this.y = _y;
}
}
}
然后是主类:
package {
import flash.display.Sprite;
import classes.*;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends Sprite {
private var goldFish:J_GoldFish;
private var blueFish:J_BlueFish;
private var bird:J_Bird;
private var fishBowl:J_FishBowl;
private var pass:String = "";
public function Main() {
// constructor code
this.goldFish = new J_GoldFish(146.80, 372.75);
this.blueFish = new J_BlueFish(59.95, 335.05);
this.bird = new J_Bird(497.35, 48.45);
this.fishBowl = new J_FishBowl(269.30, 319.50);
this.fishBowl.name = "fishBowl";
this.goldFish.name = "goldFish";
this.blueFish.name = "blueFish";
this.bird.name = "bird";
addChild(this.fishBowl);
addChild(this.goldFish);
addChild(this.blueFish);
addChild(this.bird);
stage.addEventListener(Event.ENTER_FRAME, animate);
//this.addEventListener(MouseEvent.MOUSE_OVER, sendObject);
//stage.addEventListener('DRAG_OBJECT', stopAnimate);
private function animate(evt:Event):void {
//make the fish animate (move)
this.goldFish.move();
this.blueFish.move();
this.bird.move();
}
private function stopAnimate(evt:Event):void {
//var target:J_Objects = (evt.target as J_Objects); //definde the target incase I want to change properties
//stage.removeEventListener(Event.ENTER_FRAME, animate);
}
}
}