Flash 游戏中的交叉拖动行为
我想创建一个方块游戏,你可以拖动方块。我在寻找最友好的拖拉行为。它只能水平或垂直移动,并且只能移动1个块位置。它应该按扣(容易达到目标位置),因为你不能在中间(在块之间)释放它。它应该有点像iPhone的开关按钮,但在一个十字架。我试着通过检查鼠标相对于块的原始位置的角度来检查它应该移动的方向。我希望它总是通过中心移动(原始位置)。我是说;它不能从左位置设置动画到上位置,它应该先设置动画到中心 块是左上对齐的,块之间有间距 最好的方法是什么? 更新:Flash 游戏中的交叉拖动行为,flash,actionscript-3,actionscript,Flash,Actionscript 3,Actionscript,我想创建一个方块游戏,你可以拖动方块。我在寻找最友好的拖拉行为。它只能水平或垂直移动,并且只能移动1个块位置。它应该按扣(容易达到目标位置),因为你不能在中间(在块之间)释放它。它应该有点像iPhone的开关按钮,但在一个十字架。我试着通过检查鼠标相对于块的原始位置的角度来检查它应该移动的方向。我希望它总是通过中心移动(原始位置)。我是说;它不能从左位置设置动画到上位置,它应该先设置动画到中心 块是左上对齐的,块之间有间距 最好的方法是什么? 更新: 这是我当前的代码。它当前会沿该方向移动,但
这是我当前的代码。它当前会沿该方向移动,但不会设置到该点的动画
package nl.stroep.games.behaviors
{
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import nl.stroep.games.boxgame.Setting;
/**
* Target dragging behavior
* @author Mark Knol
*/
public class CrossDragBehavior
{
private static const HALF_PI:Number = Math.PI / 2;
private static const QUART_PI:Number = HALF_PI / 2;
private var _target:DisplayObject;
private var _targetPosition:Point;
public function CrossDragBehavior(target:DisplayObject)
{
_target = target;
_target.addEventListener(MouseEvent.MOUSE_DOWN, handleMouse);
_target.stage.addEventListener(MouseEvent.MOUSE_UP, handleMouse);
}
private function handleMouse(e:MouseEvent):void
{
switch(e.type)
{
case MouseEvent.MOUSE_DOWN:
{
_targetPosition = new Point(_target.x, _target.y);
_target.addEventListener(Event.ENTER_FRAME, update);
update(null);
break;
}
case MouseEvent.MOUSE_UP:
{
_target.removeEventListener(Event.ENTER_FRAME, update);
break;
}
}
}
private function update(e:Event):void
{
var mouseX:Number = _target.parent.mouseX;
var mouseY:Number = _target.parent.mouseY;
var dx:Number = mouseX - _targetPosition.x;
var dy:Number = mouseY - _targetPosition.y;
var angle:Number = Math.atan2(dy, dx);
//trace(angle);
var directionX:int = 0;
var directionY:int = 0;
if (dx * dx + dy * dy > Setting.BLOCK_WIDTH * Setting.BLOCK_WIDTH) // minimum distance before snap
{
if (angle < 0 * HALF_PI - QUART_PI) { directionX = 0; directionY = -1; }
else if (angle < 1 * HALF_PI - QUART_PI) { directionX = 1; directionY = 0; }
else if (angle < 2 * HALF_PI - QUART_PI) { directionX = 0; directionY = 1; }
else if (angle < 3 * HALF_PI - QUART_PI) { directionX = -1; directionY = 0; }
else { directionX = 1; directionY = 0; }
}
_target.x = _targetPosition.x + (directionX * (Setting.BLOCK_WIDTH + Setting.BLOCK_DISTANCE))
_target.y = _targetPosition.y + (directionY * (Setting.BLOCK_HEIGHT + Setting.BLOCK_DISTANCE))
}
public function set target(value:DisplayObject):void
{
_target = value;
}
}
}
包nl.stroep.games.behaviors
{
导入flash.display.DisplayObject;
导入flash.events.Event;
导入flash.events.MouseEvent;
导入flash.geom.Point;
导入nl.stroep.games.boxgame.Setting;
/**
*目标拖动行为
*@作者马克·诺尔
*/
公共类CrossDragBehavior
{
私有静态常量HALF_PI:Number=Math.PI/2;
专用静态常数夸脱π:数字=半π/2;
私有变量\u目标:DisplayObject;
私人var_目标位置:点;
公共函数CrossDragBehavior(目标:DisplayObject)
{
_目标=目标;
_target.addEventListener(MouseEvent.MOUSE_DOWN,handleMouse);
_target.stage.addEventListener(MouseEvent.MOUSE_UP,handleMouse);
}
专用功能手机(e:MouseeEvent):无效
{
开关(e型)
{
case MouseEvent.MOUSE_向下:
{
_targetPosition=新点(_target.x,_target.y);
_target.addEventListener(Event.ENTER\u FRAME,update);
更新(空);
打破
}
case MouseEvent.MOUSE\u UP:
{
_target.removeEventListener(Event.ENTER_FRAME,update);
打破
}
}
}
私有函数更新(e:事件):无效
{
var mouseX:Number=\u target.parent.mouseX;
var mouseY:Number=\u target.parent.mouseY;
变量dx:Number=mouseX-_targetPosition.x;
变量dy:Number=mouseY-_targetPosition.y;
变量角度:数值=数学atan2(dy,dx);
//轨迹(角度);
var-directionX:int=0;
变量方向:int=0;
if(dx*dx+dy*dy>Setting.BLOCK\u WIDTH*Setting.BLOCK\u WIDTH)//捕捉前的最小距离
{
如果(角度<0*HALF_PI-QUART_PI){directionX=0;directionY=-1;}
else如果(角度<1*HALF_PI-QUART_PI){directionX=1;directionY=0;}
else如果(角度<2*HALF_PI-QUART_PI){directionX=0;directionY=1;}
else如果(角度<3*HALF_PI-QUART_PI){directionX=-1;directionY=0;}
else{directionX=1;directionY=0;}
}
_target.x=_targetPosition.x+(方向x*(Setting.BLOCK_WIDTH+Setting.BLOCK_DISTANCE))
_target.y=_targetPosition.y+(方向y*(Setting.BLOCK_HEIGHT+Setting.BLOCK_DISTANCE))
}
公共功能设置目标(值:DisplayObject):无效
{
_目标=价值;
}
}
}
最好的方法可能是你自己去发现
就像在任何应用程序中一样,简单地开始,将您的问题分解成块,尝试确定有问题的问题,然后实现第一个结构,足够灵活,可以进行改进
我不了解你比赛的方方面面
"cause you cannot release it in the middle" //middle of what, of the cross?
"I want it always to go move to the center." // how's that possible?
"It should only move horizontal or vertical, and 1 block position only"
"It should kinda behave like an iPhone switch button, but in a cross"
就我个人而言,我觉得所有这些句子都令人困惑。如果它只移动了一个街区,它怎么会在十字架上呢
抛开这些问题不谈,您可以从创建块类开始
它需要做什么
- Move Up
- Move Down
- Move Left
- Move Right
在这里,您可以选择实现单个方法或每个移动一个方法。您的选择:)
如果你知道积木的去向,那么很容易让它折断。使用tweening方法,您只需要一个目的地、一个延迟和一个ease函数,就可以使其平稳移动
方向也不太难,在鼠标移动中,你可以很容易地计算出方向,当你有了方向,你就有了目的地。如果你有目的地,你就有方法
总之,你的Block类可能有一些属性和方法
private var left:int = 0;
private var right:int = 1;
private var top:int = 2;
private var down:int = 3;
private var tweenDelay:Number = .5;
// event listeners
private function mouseDownListener(event:MouseEvent):void{};
private function mouseUpListener(event:MouseEvent):void{};
private function mouseMoveListener(event:MouseEvent):void{};
//get a couple of coordinates, feed them to the method &
//return an int (or a String )
private function getDirection(params:Object):int;
private function tweenBlock( direction:String ):void{}
加入一些布尔值来限制块的移动,或者使用一个矩形来避免越界。瞧!:)
我可能忘记或忽略了一些东西,但这应该让你开始
另一方面,如果您已经知道以上所有内容,并且遇到了问题,请确切地告诉我们问题所在,这可能会更有帮助。对不起,这是因为我的英语。这两个答案很重要:“因为你不能在中间释放它”你不能在两个块之间释放它,它应该卡在其中一个块上“我希望它总是移动到中心。”它应该总是穿过原始位置。这才是真正的问题。如果向左拖动块(例如),则目标位置为左块。它应该向左移动。但是当我不释放鼠标并像应该向上移动/拖动块时,块不应该向上移动,因为你应该向右移动鼠标(回到中心),然后再向上拖动。