Interaksi Obyek Pemrograman Game Eko Prasetyo Teknik Informatika Univ. Pembangunan Nasional Veteran Jawa Timur 2012
Tabrakan Obyek Skenario: Peluru yang ditembakkan, ketika mengenai obyek yang lain (misal: tikus), maka tikus akan mati, dan skor ditambah. Algoritma solusi: if (we have collided with a gun) { remove the mouse from the world; show an explosion; //Optional show final score (game over); }
Class Actor protected List getIntersectingObjects (Class cls) Return all the objects that intersect this object. protected Actor getOneIntersectingObject (Class cls) Return an object that intersects this object. protected List getObjectsInRange(int r, Class cls) Return all objects within range 'r' around this object. Catatan: Untuk List, harus menambahkan library: import java.util.List; getOneIntersectingObject getObjectsInRange
Game: Menembak Tikus Skenario: Peluru yang ditembakkan, ketika mengenai obyek yang lain (misal: tikus), maka tikus akan mati, dan skor ditambah.
Cara 1 Menggunakan method: protected Actor getOneIntersectingObject (Class cls) Kelas Tikus import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Tikus extends Actor { public void act() { // Add your action code here. kenaPeluru(); } public void kenaPeluru() { Peluru p = (Peluru) getOneIntersectingObject(Peluru.class); if (p != null) { Arena a = (Arena) getWorld(); a.updateGambar(); getWorld().addObject(new Tikus(), Greenfoot.getRandomNumber(100) + 400,Greenfoot.getRandomNumber(400)); getWorld().removeObject(this);
Kelas Peluru Cara 1 import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Peluru extends Actor { public void act() { // Add your action code here. lihatTikus(); //gerak(); } public void gerak() { setLocation(getX()+4,getY()); if (getX() >= getWorld().getWidth() - 2) { getWorld().removeObject(this); public void lihatTikus() { Tikus a = (Tikus) getOneIntersectingObject(Tikus.class); if (a != null) { } else { gerak();
Cara 2 Menggunakan method: protected List getObjectsInRange(int r, Class cls) Kelas Tikus import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Tikus extends Actor { public void act() { // Add your action code here. } public void matikan() { Arena a = (Arena) getWorld(); a.updateGambar(); getWorld().addObject(new Tikus(), Greenfoot.getRandomNumber(100) + 400,Greenfoot.getRandomNumber(400)); getWorld().removeObject(this);
Kelas Peluru Cara 2 import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List; public class Peluru extends Actor { public void act() { // Add your action code here. lihatTikus(); gerak(); } public void gerak() { setLocation(getX()+4,getY()); if (getX() >= getWorld().getWidth() - 2) { getWorld().removeObject(this); public void lihatTikus() { int range = getImage().getWidth() / 2; List<Tikus> tikus = getObjectsInRange(range, Tikus.class); for (Tikus a : tikus) { a.matikan();
Saatnya anda membuat game Tugas 2 Saatnya anda membuat game