作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试设计一款 child 手镯游戏,他们可以在其中将珠子拖放到绳子上(圆圈)但是我无法将对象捕捉到我的椭圆(圆)上,目前球(珠子)围绕椭圆旋转,但我需要它和倍数从屏幕底部拖动,然后在它靠近时能够捕捉到椭圆,基本上这样 child 就可以选择一颗珠子并将其拖到绳子上,以制作他们自己的手链,
你能帮我解决这个问题吗?下面的代码是我到目前为止所拥有的,任何帮助或朝着正确方向迈出的一步都会非常感谢! :)
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();
}
最佳答案
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();
}
关于flash - 创建 Flash 拖放珠宝游戏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8228595/
我是一名优秀的程序员,十分优秀!