Oleh: jagan21 | Juli 6, 2011

Belajar merubah rotasi dalam java

Untuk merubah rotasi perintah dasarnya adalah:

public void act()

{

int rot = getRotation() + 1;

if(rot == 360) {

rot = 0;

}

setRotation(rot);

}

Metode ini berarti mengambil object (gambar) pada rotasi sekarang dan akan selalu bertambah 1. Efeknya objek akan bergerak perlahan kearah panah.

Angka yang benar pada rotasi adalah angka 0-359. Untuk mensiasati ini dibuat pernyataan if(rot == 360) { yang dapat diartikan jika sudah mencapai rotasi 3600 maka rot=0.

Buka jendela coding (double klik pada actor snake) lalu buat pergerakan snake dengan aturan snake akan selalu bergerak sambil berotasi (berputar-putar).

import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/**
* Write a description of class snake here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class snake extends Actor
{
/**
* Act – do whatever the snake wants to do. This method is called whenever
* the ‘Act’ or ‘Run’ button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
//pergerakan snake
int rot = getRotation() + 10;
if (rot < 90) {
setLocation(getX() + 2, getY());
}
else if (rot < 180) {
setLocation(getX(), getY() + 2 );
}
else if (rot < 270) {
setLocation(getX() – 2, getY());
}
else if (rot < 360) {
setLocation(getX(), getY() – 2);
}
if (rot > 360) {
rot = 0;
}
setRotation(rot);
}
}


Tinggalkan Balasan

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Ubah )

Twitter picture

You are commenting using your Twitter account. Log Out / Ubah )

Facebook photo

You are commenting using your Facebook account. Log Out / Ubah )

Connecting to %s

Kategori

Ikuti

Get every new post delivered to your Inbox.