我有个怪物能产生水晶。我希望每个水晶围绕着怪物,但是当有不止一个晶体时,我希望它们以相等的距离运行。我一直在尝试用我已经拥有的两块代码来完成这个任务,但是每个代码都有不同的功能,我需要一段代码来完成所有的任务。
这个块只允许一个物体绕另一个物体运行:
orbitRadius = 110;
angle += orbitSpeed;
rad = (angle * (Math.PI / 180));
orbitX = monster.x + orbitRadius * Math.cos(rad);
orbitY = monster.y + orbitRadius * Math.sin(rad);这是一段关于它的样子的视频:https://www.youtube.com/watch?v=ACclpQBsjPo
此代码块根据晶体的数量将晶体围绕在怪物周围排列,有:
radius = 110;
angle = ((Math.PI * 2) / targetArray.length) * targetArray.indexOf(this);
orbitX = monster.x - (radius * Math.cos(angle));
orbitY = monster.y - (radius * Math.sin(angle));这是一段视频:https://www.youtube.com/watch?v=TY0mBHc2A8U
我不知道如何同时把晶体和和放在一起,让它们绕着怪物转。要做到这一点,需要做些什么?
发布于 2017-10-01 01:12:41
1)分层方式:将晶体放入同一个容器中,使它们均匀分布(就像在第二个视频中所做的那样),然后旋转容器。
2)数学方法。
执行情况:
public class Orbiter extends Sprite
{
// Pixels.
public var radius:Number = 100;
// Degrees per second.
public var speed:Number = 360;
public var items:Array;
public var lastTime:int;
public function start()
{
stop();
rotation = 0;
items = new Array;
lastTime = getTimer();
addEventListener(Event.ENTER_FRAME, onFrame);
}
public function stop():void
{
items = null;
removeEventListener(Event.ENTER_FRAME, onFrame);
}
public function onFrame(e:Event = null):void
{
var aTime:int = getTimer();
rotation += speed * (aTime - lastTime) / 1000;
lastTime = aTime;
for (var i:int = 0; i < items.length; i++)
{
// Get the object.
var anItem:DisplayObject = items[i];
// Get the object's designated position.
var aPos:Point = getPosition(i);
// Follow the position smoothly.
anItem.x += (aPos.x - anItem.x) / 10;
anItem.y += (aPos.y - anItem.y) / 10;
}
}
private function getPosition(index:int):Point
{
// Calculate the angle with regard to the present items amount.
var anAngle:Number = (rotation - 360 / items.length) * Math.PI / 180;
var result:Point = new Point;
// Figure the position with regard to (x,y) offset.
result.x = x + radius * Math.cos(anAngle);
result.y = y + radius * Math.sin(anAngle);
return result;
}
}用法:
var O:Orbiter = new Orbiter;
// Define the offset.
O.x = monster.x;
O.y = monster.y;
// Set radius and rotation speed.
O.radius = 110;
O.speed = 270;
// Enable the rotation processing.
O.start();
// Append items to orbit.
O.items.push(Crystal1);
O.items.push(Crystal2);
O.items.push(Crystal3);您可以随时更改radius和 fine ,以及添加/删除项,这要感谢运动平滑,这一切看起来都一样好。
https://stackoverflow.com/questions/46508123
复制相似问题