I would like to rotate this SpriteBatch on itself upon clicking on a button
#Override
public void render() {
SpriteBatch batch = new SpriteBatch();
batch.begin();
batch.draw(gemTexture, 10, 10, 100, 100);
batch.end();
if (Gdx.input.isTouched()) {
rotateRight();
}
}
private void rotateRight() {
// How do I rotate it to look like
}
You're drawing a Texture using a SpriteBatch. A Texture doesn't support rotation. I suggest that the Sprite class might be better suited for what you are trying to do. Here's a rough outline of what you might do... see the Sprite javadoc for more detail.
private void createGemSprite() {
gemSprite = new Sprite(gemTexture);
gemSprite.setPosition(10, 10);
}
#Override
public void render() {
SpriteBatch batch = new SpriteBatch();
batch.begin();
gemSprite.draw(batch);
batch.end();
if (Gdx.input.isTouched()) {
rotateRight();
}
}
private void rotateRight() {
gemSprite.setRotation(gemSprite.getRotation() - 90);
}
Related
QUESTION
I'm trying to create a simple object "car" animation in Processing.
My code is:
Car car;
void setup(){
size(800, 600);
background(#818B95);
frameRate(30);
car = new Car(10,10);
}
void draw(){
//refresh background
background(#818B95);
print("-");
car.drawCar();
}
void mouseClicked() {
car.run();
}
public class Car implements Runnable{
private int pos_x, pos_y;
public Car(int pos_x, int pos_y){
this.pos_x = pos_x;
this.pos_y = pos_y;
}
public void drawCar(){
rect(pos_x,pos_y,10,10);
}
public void run(){
while(true){
pos_x += 10;
print("*");
delay(200);
}
}
}
I'm expecting to see the car/rectangle move right when I click the mouse button, but nothing happens.
I've added the two print in order to see if the draw method and my car.run are executed in parallel showing some * and - printed alternately.
What I see is a sequence of - until I click and then only * are printed.
Is it possible that starting a new object thread will stop the main draw cycle?
SOLUTION
This is just a variant of the suggested solution (by Mady Daby) without using threads.
Car car;
void setup(){
size(800, 600);
background(#818B95);
frameRate(30);
car = new Car(10,10);
}
void draw(){
//refresh background
background(#818B95);
print("-");
car.drawCar();
}
void mouseClicked() {
car.moving = true;
}
public class Car{
private int pos_x, pos_y;
boolean moving = false;
public Car(int pos_x, int pos_y){
this.pos_x = pos_x;
this.pos_y = pos_y;
}
public void drawCar(){
rect(pos_x,pos_y,10,10);
//animation
if(moving){
pos_x += 10;
print("*");
}
}
}
You could make the car move right by just introducing a boolean variable to track whether the car is supposed to be moving right moving and then increment pos_x if moving is true. You can also use the clicks to toggle between moving and not moving.
Car car;
void setup() {
size(800, 600);
background(#818B95);
frameRate(30);
car = new Car(10, 10);
}
void draw() {
//refresh background
background(#818B95);
print("-");
car.drawCar();
}
void mouseClicked() {
car.toggleMoving();
}
public class Car implements Runnable {
private int pos_x, pos_y;
private boolean moving = false;
public Car(int pos_x, int pos_y) {
this.pos_x = pos_x;
this.pos_y = pos_y;
}
public void toggleMoving() {
moving = !moving;
}
public void drawCar() {
if (moving) {
this.run();
}
rect(pos_x, pos_y, 10, 10);
}
public void run() {
pos_x += 10;
print("*");
delay(200);
}
}
What's the best way to change the playback speed of an mp3 file, going gradually from 1.0 to 1.5 without interrupts of the sound, nor clear steps of increases being noticed by the listener?
I played around with the setRate, but I don't get things working smoothly.
public class AudioSyncTest extends Application {
private String song;
private Media songSound;
private MediaPlayer mediaPlayer;
Pane pane = new Pane();
#Override
public void start(Stage stage) throws Exception {
song = "/audio/I love the rain.mp3";
songSound = new Media((getClass().getResource(song).toString()));
mediaPlayer = new MediaPlayer(songSound);
mediaPlayer.seek(Duration.ZERO);
mediaPlayer.setRate(1);
mediaPlayer.play();
Button b = new Button("Speed up smoothly..");
b.setOnAction(actionEvent ->
{
backgroundTask();
});
pane.getChildren().add(b);
Scene scene = new Scene(pane, 280, 200);
stage.setScene(scene);
stage.show();
}
I tried with a background task... but no luck there either.
public void backgroundTask() {
Task<Void> task = new Task<>() {
double currentSpeed = 1;
double increment = 0.01;
#Override
public Void call() {
for (int i = 0; i < 50; i++) {
try {
Thread.sleep(100);
currentSpeed+= increment;
mediaPlayer.setRate(currentSpeed);
System.out.println("incremented: " + currentSpeed);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//mediaPlayer.setRate(ThreadLocalRandom.current().nextDouble(0.5, 1.5));
return null;
}
};
new Thread(task).start();
}
Any help would be appreciated!
Kind regards,
Maarten
I want to resize a UI panel with the mouse. I don't know how to do this myself. How can I do this?
Kind of a short question, so I'm not sure if you want anything extremely specific, but this is something I found on the Unity Tutorials Website. Seems to be what you're looking for:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ResizePanel : MonoBehaviour, IPointerDownHandler, IDragHandler {
public Vector2 minSize;
public Vector2 maxSize;
private RectTransform rectTransform;
private Vector2 currentPointerPosition;
private Vector2 previousPointerPosition;
void Awake () {
rectTransform = transform.parent.GetComponent<RectTransform>();
}
public void OnPointerDown (PointerEventData data) {
rectTransform.SetAsLastSibling();
RectTransformUtility.ScreenPointToLocalPointInRectangle (rectTransform, data.position, data.pressEventCamera, out previousPointerPosition);
}
public void OnDrag (PointerEventData data) {
if (rectTransform == null)
return;
Vector2 sizeDelta = rectTransform.sizeDelta;
RectTransformUtility.ScreenPointToLocalPointInRectangle (rectTransform, data.position, data.pressEventCamera, out currentPointerPosition);
Vector2 resizeValue = currentPointerPosition - previousPointerPosition;
sizeDelta += new Vector2 (resizeValue.x, -resizeValue.y);
sizeDelta = new Vector2 (
Mathf.Clamp (sizeDelta.x, minSize.x, maxSize.x),
Mathf.Clamp (sizeDelta.y, minSize.y, maxSize.y)
);
rectTransform.sizeDelta = sizeDelta;
previousPointerPosition = currentPointerPosition;
}
}
I have created a CollapsablePane which shows an EffectImageView as collapsableContent, and a Node as mainContent. The EffectImageView consists of two layered ImageViews, the bottom one having an BoxBlur effect applied on it.
When there is a ScrollEvent on the mainContent the collapsableContent will be animated accordingly by setTranslateY.
With a BoxBlur effect applied, the translation of the lower part of the collapsableContent is very unsmooth, while it is absolutely smooth without the effect.
Is there a possibility to improve the transition performance?
public class EffectImageView extends Pane {
private ImageView img;
private ImageView imgEffect;
private Rectangle clipTop;
private Rectangle clipBottom;
private DoubleProperty imageEffectHeight = new SimpleDoubleProperty();
private double effectOffset;
public EffectImageView() {
img = createCachedImageView();
imgEffect = createCachedImageView();
imgEffect.imageProperty().bind(img.imageProperty());
imgEffect.fitWidthProperty().bind(img.fitWidthProperty());
imgEffect.fitHeightProperty().bind(img.fitHeightProperty());
clipTop = new Rectangle();
clipTop.widthProperty().bind(img.fitWidthProperty());
clipTop.heightProperty().bind(img.fitHeightProperty().subtract(imageEffectHeight).subtract(translateYProperty()));
clipBottom = new Rectangle();
clipBottom.widthProperty().bind(img.fitWidthProperty());
clipBottom.heightProperty().bind(imageEffectHeight);
clipBottom.translateYProperty().bind(heightProperty().subtract(imageEffectHeight).subtract(translateYProperty()));
img.setClip(clipTop);
imgEffect.setClip(clipBottom);
getChildren().addAll(img, imgEffect);
}
private ImageView createCachedImageView() {
ImageView img = new ImageView();
img.setCache(true);
img.setCacheHint(CacheHint.SPEED);
return img;
}
public final ObjectProperty<Image> imageProperty() {
return img.imageProperty();
}
public final Image getImage() {
return img.getImage();
}
public final void setImage(Image image) {
img.setImage(image);
}
public void setImageEffect(Effect effect) {
imgEffect.setEffect(effect);
}
public final DoubleProperty imageEffectHeightProperty() {
return imageEffectHeight;
}
public void setEffectOffset(double offset) {
effectOffset = offset;
}
#Override
protected void layoutChildren() {
double w = getWidth();
double h = getHeight();
img.relocate(-effectOffset, 0);
imgEffect.relocate(-effectOffset, 0);
img.setFitWidth(w + effectOffset * 2);
img.setFitHeight(h);
}
}
I want to rotate my ImageView 2 times
Initially it will rotate 360 degree and after that extra some degree..
I am using following code
protected void rotation(int start, int end, int time) {
Animation a = new RotateAnimation((float)start, (float)end,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
a.setDuration((long)time);
a.setInterpolator(new LinearInterpolator());
a.setFillAfter(true);
ImageView.startAnimation(a);
}
My problem is
I am calling this function 2 times so it doesn't waits for first and directly starts second rotation.
I want to wait until first rotation completes and then start second rotation
Set a Listener in first animation
a.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
callNewAnimation();
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});