[英]Can I let people draw with the cursor using HaxeFlixel?
基本上是這個問題。 我正在嘗試制作一款游戲,您需要繪制某些形狀才能執行某些操作。 我想讓游戲追蹤 cursor 的移動,但我找不到任何制作線條的好方法。
我試過使用FlxSpriteUtil.drawLine
,但這似乎不起作用。 我也嘗試過使用FlxSpriteGroups.clone()
將一個點克隆為 cursor,但這只會留下一串點,所以這也不起作用。
我真的是 Haxe 和 HaxeFlixel 的新手,所以我不知道該做什么或使用什么。 有什么建議么?
聽起來您是通過使用FlxSpriteUtil
中的drawLine
開始的。 制作可繪制線時,最簡單的方法是將之前的鼠標 position 保存在一個變量中,然后每幀從之前的 position 到當前的 position 繪制一條線(即將代碼放在update()
)。
這是一個小代碼示例,它創建了一個允許繪圖的canvas
精靈。
package;
import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.math.FlxPoint;
import flixel.util.FlxColor;
using flixel.util.FlxSpriteUtil;
class PlayState extends FlxState
{
private var canvas:FlxSprite;
private var lastPoint:FlxPoint;
private static inline var CANVAS_OFFSET = 16;
private static inline var DRAW_THICKNESS = 2;
private static inline var DRAW_COLOR = FlxColor.BLACK;
override public function create()
{
super.create();
canvas = new FlxSprite(CANVAS_OFFSET, CANVAS_OFFSET);
canvas.makeGraphic(256, 256, FlxColor.WHITE);
add(canvas);
}
override public function update(elapsed:Float)
{
super.update(elapsed);
if (FlxG.mouse.pressed && FlxG.mouse.overlaps(canvas))
{
if (FlxG.mouse.justPressed)
{
// This is the first point, i.e. the beginning of a new stroke
lastPoint = FlxG.mouse.getPosition();
// First, create an ellipsis to enable the player to click to draw a line
canvas.drawEllipse(
FlxG.mouse.x - CANVAS_OFFSET,
FlxG.mouse.y - CANVAS_OFFSET,
DRAW_THICKNESS / 2,
DRAW_THICKNESS / 2,
DRAW_COLOR
);
}
else
{
// Otherwise, line to the previously drawn point
var mousePos = FlxG.mouse.getPosition();
canvas.drawLine(
lastPoint.x - CANVAS_OFFSET,
lastPoint.y - CANVAS_OFFSET,
mousePos.x - CANVAS_OFFSET,
mousePos.y - CANVAS_OFFSET,
{
thickness: DRAW_THICKNESS,
color: DRAW_COLOR
}
);
lastPoint = mousePos;
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.