AS3 - 如果符号的坐标到达此处?



我正在使用Flash Professional CS5.5,我需要制作一个应用程序,其中有一个球(符号)使用加速度计移动,我希望当球坐标A到达此坐标B时,它会转到第2帧(gotoAndPlay(2))。我得先找到球坐标,对吧?我该怎么做?

这是我现在的代码

c_ball.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);
function fl_ClickToDrag(event:MouseEvent):void{
c_ball.startDrag();}
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);
function fl_ReleaseToDrop(event:MouseEvent):void{
c_ball.stopDrag();}

如果在检索坐标后会起作用吗?

function f_level (e) if (c_ball.x==100 && c_ball.y==100) {
gotoAndStop(2);}
如果您

正在寻找加速度计数据,MOUSE_UP和MOUSE_DOWN不是您需要的。您需要加速度计类和关联的事件。

尝试这样的事情:

import flash.sensors.Accelerometer;
import flash.events.AccelerometerEvent;
var accel:Accelerometer = new Accelerometer();

accel.addEventListener(AccelerometerEvent.UPDATE, handleAccelUpdate);

更新处理程序:

function handleAccelUpdate(e:AccelerometerEvent):void{
   //inside this function you now have access to acceleration x/y/z data
   trace("x: " + e.accelerationX);
   trace("y: " + e.accelerationY);
   trace("z: " + e.accelerationZ);
   //using this you can move your MC in the correct direction
   c_ball.x -= (e.accelerationX * 10); //using 10 as a speed multiplier, play around with this number for different rates of speed 
   c_ball.y += (e.accelerationY * 10); //same idea here but note the += instead of -=
   //you can now check the x/y of your c_ball mc
   if(c_ball.x == 100 && c_ball.y == 100){
         trace("you win!"); //fires when c_ball is at 100, 100
   }
} 

现在,这将允许您将MC"滚出"屏幕,因此您可能希望添加某种边界检查。

查看这篇精彩的文章以获取更多信息:

http://www.republicofcode.com/tutorials/flash/as3accelerometer/

一种简单而节省的方法是使用碰撞检测,而不是测试一个位置(用户很难满足),而是选择目标区域:

package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class Hittester extends Sprite
{
    var ball:Sprite = new Sprite();
    var testarea:Sprite = new Sprite();
    public function Hittester()
    {
        super();
        ball.graphics.beginFill(0xff0000);
        ball.graphics.drawCircle(0,0,10);
        testarea.graphics.beginFill(0x00ff00);
        testarea.graphics.drawRect(0,0,50,50);
        testarea.x = 100;
        testarea.y = 100;
        // if testarea should be invisble
        /*testarea.alpha = 0;
        testarea.mouseEnabled = false;
        */
        ball.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
        addChild(testarea);
        addChild(ball);
    }
    private function startDragging( E:Event  = null):void{
        ball.startDrag();
        stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
    }
    private function stopDragging( E:Event  = null):void{
        stage.removeEventListener(MouseEvent.MOUSE_UP, stopDragging);       
        ball.stopDrag();
        test();
    }
    private function test():void{
        if( ! ball.hitTestObject(testarea) ){
            ball.x = 10;
            ball.y = 10;
        }
        else{
            // here goes next frame command ;)
        }
    }   
}
}

相关内容

最新更新