我在舞台上有两张图片......根据下面的代码,我可以选择并IMG1
拖入IMG2
,迫使第二个IMG2
水平移动,同时有一个检测到碰撞。 但是,问题在于IMG2不会在其碰撞检测中让IMG1变形。我看不出我做错了什么。
另外,如果我想在舞台上使用2个以上的IMG来运行这个测试怎么样呢?它们的完整数组如何具有拖放功能并运行hittest来逐个推动它们。
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="test()">
<mx:Script>
<![CDATA[
import flash.display.Stage;
import flash.display.Sprite;
import model.ModelLocator;
public var modelLocator:ModelLocator = ModelLocator.getInstance();
[Bindable]
public var mouseXCoords:int = 0;
[Bindable]
public var mouseYCoords:int = 0;
public var yOffset:int = 70;
[Bindable]
public var holderMid:int = 147;
public function hitTestIMG1(yourEvent:Event):void
{
if(IMG1.hitTestObject(IMG2)) {
if(IMG1.x < IMG2.x-(IMG2.width/2)) {
trace("LEFT SIDE!!!");
trace("IMG1: " + IMG1.x);
trace("IMG1 Width: " + IMG1.width);
trace("IMG2: " + IMG2.x);
trace("IMG2 Width: " + IMG2.width);
IMG2.x = IMG1.x+(IMG1.width);
} else
if(IMG1.x > IMG2.x+(IMG2.width/2)) {
trace("RIGHT SIDE!!!");
trace("IMG1: " + IMG1.x);
trace("IMG1 Width: " + IMG1.width);
trace("IMG2: " + IMG2.x);
trace("IMG2 Width: " + IMG2.width);
IMG2.x = IMG1.x-(IMG2.width);
}
}
}
public function hitTestIMG2(yourEvent:Event):void
{
if(IMG2.hitTestObject(IMG1)) {
if(IMG2.x < IMG1.x-(IMG1.width/2)) {
trace("LEFT SIDE!!!");
trace("IMG1: " + IMG1.x);
trace("IMG1 Width: " + IMG1.width);
trace("IMG2: " + IMG2.x);
trace("IMG2 Width: " + IMG2.width);
IMG1.x = IMG2.x+(IMG2.width);
} else
if(IMG2.x > IMG1.x+(IMG1.width/2)) {
trace("RIGHT SIDE!!!");
trace("IMG1: " + IMG1.x);
trace("IMG1 Width: " + IMG1.width);
trace("IMG2: " + IMG2.x);
trace("IMG2 Width: " + IMG2.width);
IMG1.x = IMG2.x-(IMG1.width);
}
}
}
public function IMG1Start(IMGMove:Event):void
{
IMG1.startDrag();
}
public function IMG1Stop(IMGStop:Event):void
{
IMG1.stopDrag();
// check to see the closeness to the bracelet
if(IMG1.y < (holderMid + yOffset) && IMG1.y > (holderMid - yOffset))
{
trace("SNAP!");
IMG1.y=holderMid; // set it to bracelet
} else {
// just stop dragging
}
}
public function IMG2Start(IMGMove:Event):void
{
IMG2.startDrag();
}
public function IMG2Stop(IMGStop:Event):void
{
IMG2.stopDrag();
if(IMG2.y < (holderMid + yOffset) && IMG2.y > (holderMid - yOffset))
{
trace("SNAP!");
IMG2.y=holderMid; // set it to bracelet
} else {
// just stop dragging
}
}
public function mouseCoords(mouseUpdate:Event):void
{
mouseXCoords = myCanvas.mouseX;
mouseYCoords = myCanvas.mouseY;
}
public function test():void
{
// stage listeners - must be set on startup!!!
modelLocator.currentIMGs.addItem(IMG1);
modelLocator.currentIMGs.addItem(IMG2);
IMG1.addEventListener(Event.ENTER_FRAME, hitTestIMG1);
IMG2.addEventListener(Event.ENTER_FRAME, hitTestIMG2);
stage.addEventListener(Event.ENTER_FRAME, mouseCoords);
mouseXCoord.text = mouseXCoords as String;
mouseYCoord.text = mouseYCoords as String;
}
]]>
</mx:Script>
<mx:Canvas x="144" y="100" width="863" height="386"
borderColor="white" borderThickness="3"
id="myCanvas"
backgroundImage="assets/test.jpg">
<mx:Image x="252" y="147" width="76" id="IMG1" buttonMode="true"
source="assets/IMG8.png"
mouseDown="IMG1Start(event)"
mouseUp="IMG1Stop(event)"
mouseOut="IMG1Stop(event)"/>
<mx:Image x="429" y="147" id="IMG2" buttonMode="true"
source="assets/IMG12.png"
mouseDown="IMG2Start(event)"
mouseUp="IMG2Stop(event)"
mouseOut="IMG2Stop(event)"/>
</mx:Canvas>
<mx:Label x="144" y="27" text="Mouse X: " width="64"/>
<mx:Label x="144" y="53" text="Mouse Y: " width="64"/>
<mx:Label x="216" y="27" id="mouseXCoord" text="{mouseXCoords}"/>
<mx:Label x="216" y="53" id="mouseYCoord" text="{mouseYCoords}"/>
</mx:Application>
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="test()">
<mx:Script>
<![CDATA[
import flash.display.Stage;
import flash.display.Sprite;
import model.ModelLocator;
public var modelLocator:ModelLocator = ModelLocator.getInstance();
[Bindable]
public var mouseXCoords:int = 0;
[Bindable]
public var mouseYCoords:int = 0;
public var yOffset:int = 70;
[Bindable]
public var holderMid:int = 147;
public function hitTestIMG1(yourEvent:Event):void
{
if(IMG1.hitTestObject(IMG2)) {
if(IMG1.x < IMG2.x-(IMG2.width/2)) {
trace("LEFT SIDE!!!");
trace("IMG1: " + IMG1.x);
trace("IMG1 Width: " + IMG1.width);
trace("IMG2: " + IMG2.x);
trace("IMG2 Width: " + IMG2.width);
IMG2.x = IMG1.x+(IMG1.width);
} else
if(IMG1.x > IMG2.x+(IMG2.width/2)) {
trace("RIGHT SIDE!!!");
trace("IMG1: " + IMG1.x);
trace("IMG1 Width: " + IMG1.width);
trace("IMG2: " + IMG2.x);
trace("IMG2 Width: " + IMG2.width);
IMG2.x = IMG1.x-(IMG2.width);
}
}
}
public function hitTestIMG2(yourEvent:Event):void
{
if(IMG2.hitTestObject(IMG1)) {
if(IMG2.x < IMG1.x-(IMG1.width/2)) {
trace("LEFT SIDE!!!");
trace("IMG1: " + IMG1.x);
trace("IMG1 Width: " + IMG1.width);
trace("IMG2: " + IMG2.x);
trace("IMG2 Width: " + IMG2.width);
IMG1.x = IMG2.x+(IMG2.width);
} else
if(IMG2.x > IMG1.x+(IMG1.width/2)) {
trace("RIGHT SIDE!!!");
trace("IMG1: " + IMG1.x);
trace("IMG1 Width: " + IMG1.width);
trace("IMG2: " + IMG2.x);
trace("IMG2 Width: " + IMG2.width);
IMG1.x = IMG2.x-(IMG1.width);
}
}
}
public function IMG1Start(IMGMove:Event):void
{
IMG1.startDrag();
}
public function IMG1Stop(IMGStop:Event):void
{
IMG1.stopDrag();
// check to see the closeness to the bracelet
if(IMG1.y < (holderMid + yOffset) && IMG1.y > (holderMid - yOffset))
{
trace("SNAP!");
IMG1.y=holderMid; // set it to bracelet
} else {
// just stop dragging
}
}
public function IMG2Start(IMGMove:Event):void
{
IMG2.startDrag();
}
public function IMG2Stop(IMGStop:Event):void
{
IMG2.stopDrag();
if(IMG2.y < (holderMid + yOffset) && IMG2.y > (holderMid - yOffset))
{
trace("SNAP!");
IMG2.y=holderMid; // set it to bracelet
} else {
// just stop dragging
}
}
public function mouseCoords(mouseUpdate:Event):void
{
mouseXCoords = myCanvas.mouseX;
mouseYCoords = myCanvas.mouseY;
}
public function test():void
{
// stage listeners - must be set on startup!!!
modelLocator.currentIMGs.addItem(IMG1);
modelLocator.currentIMGs.addItem(IMG2);
IMG1.addEventListener(Event.ENTER_FRAME, hitTestIMG1);
IMG2.addEventListener(Event.ENTER_FRAME, hitTestIMG2);
stage.addEventListener(Event.ENTER_FRAME, mouseCoords);
mouseXCoord.text = mouseXCoords as String;
mouseYCoord.text = mouseYCoords as String;
}
]]>
</mx:Script>
<mx:Canvas x="144" y="100" width="863" height="386"
borderColor="white" borderThickness="3"
id="myCanvas"
backgroundImage="assets/test.jpg">
<mx:Image x="252" y="147" width="76" id="IMG1" buttonMode="true"
source="assets/IMG8.png"
mouseDown="IMG1Start(event)"
mouseUp="IMG1Stop(event)"
mouseOut="IMG1Stop(event)"/>
<mx:Image x="429" y="147" id="IMG2" buttonMode="true"
source="assets/IMG12.png"
mouseDown="IMG2Start(event)"
mouseUp="IMG2Stop(event)"
mouseOut="IMG2Stop(event)"/>
</mx:Canvas>
<mx:Label x="144" y="27" text="Mouse X: " width="64"/>
<mx:Label x="144" y="53" text="Mouse Y: " width="64"/>
<mx:Label x="216" y="27" id="mouseXCoord" text="{mouseXCoords}"/>
<mx:Label x="216" y="53" id="mouseYCoord" text="{mouseYCoords}"/>
</mx:Application>