0

我正在使用 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);}
4

2 回答 2

0

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

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 ;)
        }
    }   
}
}
于 2012-04-24T17:58:43.657 回答
0

如果您正在寻找加速度计数据,则不需要 MOUSE_UP 和 MOUSE_DOWN。您需要 Accelerometer 类和相关事件。

尝试这样的事情:

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/

于 2012-04-24T17:42:18.283 回答