如何在事件上转圈? [英] How do I make a circle move on events?
问题描述
我对javafx还是很陌生,所以我想在这里学习,所以请保持理性,不要忽略我的问题,我真的很感谢任何帮助,谢谢!
I'm pretty new to javafx so I'm trying to learn here so please be reasonable and don't dis away my question, I really appreciate any help at all, thanks!
我想知道如何移动对象,比方说在不同事件(例如按键或鼠标单击,mousemove等)上的这个圆圈.
I would like to know how I could move an object, let's say this circle on different events, like keypress or mouseclick, mousemove, whatever.
Circle circle = new Circle();
circle.setCenterX(100.0f);
circle.setCenterY(100.0f);
circle.setRadius(50.0f);
我是否需要使用我在javafx网站教程上看到的KeyFrame东西,或者它如何工作?
Do I need to use that KeyFrame thing I saw on the javafx site tutorial, or how does this work?
老实说,如果我没有那么迷茫,我不会在这里问这个. 因此,请澄清一下:通过使用事件移动我创建的对象的代码是什么?
I would not have asked this here if I weren't feeling so lost, honestly. So to make this clear: What is the code for moving objects that I created, by using events?
移动是指按向上键,它向上移动几个像素,或者用另一个键对其进行变换,或者单击场景中的某个位置,使其立即移动到该位置或以一定速度移动到那里.我希望不必像使用html5 canvas一样重绘它,对吧?
By moving it I mean, press up key and it moves up by a few pixels, transform it maybe, with another key, or click somewhere on the scene and make it move there instantly or travel there with a certain speed. I don't have to redraw it like you need to with html5 canvas, I hope, right?
推荐答案
我不需要像使用html5 canvas那样重绘它,我希望是吧?
I don't have to redraw it like you need to with html5 canvas, I hope, right?
如果您使用的是标准 JavaFX场景图,则不会与 JavaFX画布相对.
Not if you are using a standard JavaFX scene graph as opposed to a JavaFX canvas.
我想知道如何移动对象,比如说在不同事件上的圆圈,例如按键或鼠标单击,鼠标移动等.
I would like to know how I could move an object, let's say this circle on different events, like keypress or mouseclick, mousemove, whatever
有三种方法可以移动形状:
- You can adjust the shape's geometry (e.g. the centerX/centerY properties of a circle).
- You can adjust the shape's layout (e.g. it's layoutX/layoutY properties).
- You can adjust the shape's translation (e.g. it's translateX/translateY properties).
您可以将布局视为对象的原始位置;即通常应该在其父组的上下文中.您可以将其视为对象的临时位置(通常在对对象进行动画处理时使用).
You can think of the layout as the home position for the object; i.e. where it should normally be in the context of it's parent group. You can think of it's translation transform as a temporary position for an object (often used when the object is being animated).
如果您使用的是布局窗格,例如 VBox 或 TilePane ,然后布局窗格将为您设置子节点的布局坐标.如果您使用的是简单的 Group 或普通 Pane 或
If you are using a layout pane such as a VBox or TilePane, then the layout pane will handle setting the layout co-ordinates of the child node for you. If you are using a simple Group or a plain Pane or Region, then you are responsible for setting the correct layout values for the child nodes.
要监听事件,请在节点或这是一个小型示例应用,它演示了上述内容.它将对象移动到Group
内,并根据各种事件修改对象在Group
内的位置.
Here is a small sample app which demonstrates the above. It places the object to be moved inside a Group
and modifies the position of the object within a Group
in response to various events.
这篇关于如何在事件上转圈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!