制作闪光拖放珠宝游戏?

时间:2011-11-22 14:40:27

标签: flash actionscript-3

我正在尝试设计一个儿童手镯游戏,他们可以将珠子拖放到一根绳子上(圆圈) 但是我无法将物体贴在我的椭圆上(圆圈), 目前,球(珠子)围绕椭圆旋转, 但是我需要它和倍数来从屏幕底部拖动​​,然后能够在它靠近时捕捉到椭圆, 基本上这样孩子可以选择一个珠子并将其拖到一个绳子上,以创建他们自己的手镯,

你能帮我一点吗?下面的代码是我到目前为止, 任何帮助或正确方向的步骤都会非常感谢你! :)

import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
var rX:Number = 150;
var rY:Number = 150;
var numItems:Number = 12;
var zeroSector:Object = sector(0, rX, rY);

function sector(degree:Number, radiusX:Number, radiusY:Number):Object {
    // coordinates of a point on ellipse
    var xpos:Number = radiusX * Math.cos(degree * Math.PI / 180);
    var ypos:Number = radiusY * Math.sin(degree * Math.PI / 180);

    // find the angle that has the Y-coords of the mouse position, solve for angle somehow...
    return {x:xpos, y:ypos};
}


function objPosition():void { // detecting coordinates of a clip
        var ratio:Number = rX / rY;

        var anAngle = Math.atan2(mouseX - myEllipse.x, mouseY - myEllipse.y );
           var deg:Number = 90-(Math.atan2(Math.sin(anAngle), Math.cos(anAngle) * ratio)) * (180 /  Math.PI);

        var _sector:Object = sector(deg, rX, rY);
        trace(_sector.x)
        bntMc.x = _sector.x;
        bntMc.y = _sector.y;

}

// draw ellipse
var myEllipse:Sprite = new Sprite();
myEllipse.x = 275;
myEllipse.y = 200;
myEllipse.graphics.lineStyle(0, 0xAAAAAA);
myEllipse.graphics.moveTo(zeroSector.x, zeroSector.y);

for (var d:int = 0; d <= 360; d += 5) {
    var _sector:Object = sector(d, rX, rY);
    myEllipse.graphics.lineTo(_sector.x, _sector.y);
}


addChild(myEllipse);

// my object
var bntMc:Sprite = new Sprite();
bntMc.graphics.lineStyle(50, 0x002277, 100);
bntMc.graphics.lineTo(0, 1);
bntMc.x = zeroSector.x;
bntMc.y = zeroSector.y;
bntMc.buttonMode = true;
myEllipse.addChild(bntMc);


bntMc.addEventListener(MouseEvent.MOUSE_DOWN, doMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP, doMouseOut);

function doMouseDown(event:MouseEvent):void  {
     stage.addEventListener(MouseEvent.MOUSE_MOVE, doMouseMove);       
};

function  doMouseOut (event:MouseEvent):void {
    stage.removeEventListener(MouseEvent.MOUSE_MOVE, doMouseMove);
 };

 function doMouseMove(event:MouseEvent):void {
     objPosition();
 }

1 个答案:

答案 0 :(得分:0)

import flash.display.Sprite; 
import flash.events.Event; 
import flash.events.MouseEvent; 
var rX:Number = 150; 
var rY:Number = 150; 
var numItems:Number = 12; 
var zeroSector:Object = sector(0, rX, rY); 

function sector(degree:Number, radiusX:Number, radiusY:Number):Object { 
    // coordinates of a point on ellipse 
    var xpos:Number = radiusX * Math.cos(degree * Math.PI / 180); 
    var ypos:Number = radiusY * Math.sin(degree * Math.PI / 180); 

    // find the angle that has the Y-coords of the mouse position, solve for angle somehow... 
    return {x:xpos, y:ypos}; 
} 


function objPosition():void { // detecting coordinates of a clip 
        var ratio:Number = rX / rY; 

        var anAngle = Math.atan2(mouseX - myEllipse.x, mouseY - myEllipse.y ); 
           var deg:Number = 90-(Math.atan2(Math.sin(anAngle), Math.cos(anAngle) * ratio)) * (180 /  Math.PI); 

        var _sector:Object = sector(deg, rX, rY); 
        trace(_sector.x) 
        bntMc.x = _sector.x; 
        bntMc.y = _sector.y; 

} 

// draw ellipse 
var myEllipse:Sprite = new Sprite(); 
myEllipse.x = 275; 
myEllipse.y = 200; 
myEllipse.graphics.lineStyle(0, 0xAAAAAA); 
myEllipse.graphics.moveTo(zeroSector.x, zeroSector.y); 

for (var d:int = 0; d <= 360; d += 5) { 
    var _sector:Object = sector(d, rX, rY); 
    myEllipse.graphics.lineTo(_sector.x, _sector.y); 
} 


addChild(myEllipse); 

// my object 
var bntMc:Sprite = new Sprite(); 
bntMc.graphics.lineStyle(50, 0x002277, 100); 
bntMc.graphics.lineTo(0, 1); 
bntMc.x = zeroSector.x; 
bntMc.y = zeroSector.y; 
bntMc.buttonMode = true; 
myEllipse.addChild(bntMc); 


bntMc.addEventListener(MouseEvent.MOUSE_DOWN, doMouseDown); 
stage.addEventListener(MouseEvent.MOUSE_UP, doMouseOut); 

function doMouseDown(event:MouseEvent):void  { 
     //stage.addEventListener(MouseEvent.MOUSE_MOVE, doMouseMove);
     bntMc.startDrag();
}; 

function  doMouseOut (event:MouseEvent):void { 
    //stage.removeEventListener(MouseEvent.MOUSE_MOVE, doMouseMove); 
    bntMc.stopDrag();
    objPosition();
 }; 

 function doMouseMove(event:MouseEvent):void { 
     objPosition();
 }