This site requires JavaScript, please enable it in your browser!
Greenfoot back
Blakehammond
Blakehammond wrote ...

2013/3/12

Need help with programming the AI

1
2
Blakehammond Blakehammond

2013/3/12

#
Could someone please post the coding to make the AI (computer controlled player) to move randomly and kill the player on impact?
Game/maniac Game/maniac

2013/3/12

#
if(Greenfoot.getRandomNumber(how often you want the action to happen, 2=very often 100+=less often)==0)
{
    //action goes here
}
Blakehammond Blakehammond

2013/3/12

#
Is that the full coding to get the character to move randomly and kill on impact? as it seems a little short
Game/maniac Game/maniac

2013/3/12

#
I gave you an start. The thing I posted will let your character do something randomly
Game/maniac Game/maniac

2013/3/12

#
To make it kill on impact, in the players code you should put:
public void act()
{
    Actor actor = getOneIntersectingObject(enemy.class);
    if(actor != null)
    {
        getWorld().removeObject(this);
    }
}
Blakehammond Blakehammond

2013/3/12

#
Does that move the character too?
Game/maniac Game/maniac

2013/3/12

#
if(Greenfoot.getRandomNumber(5)==0)  
{  
    setLocation(getX()+5, getY());
}
That would randomly move the character right
Game/maniac Game/maniac

2013/3/12

#
public void act()  
{  
    Actor actor = getOneIntersectingObject(enemy.class);  
    if(actor != null)  
    {  
        getWorld().removeObject(this);  
    }  
}
Actor actor = getOneIntersectingObject(enemy.class); if(actor != null) this part checks if there is an object from the enemy class getWorld().removeObject(this); this will remove the character if it has been touched by the object from the enemy class
Game/maniac Game/maniac

2013/3/12

#
public void act()
{
    if(Greenfoot.getRandomNumber(5)==0)    
    {    
        setLocation(getX()+5, getY());  
    }
}
Greenfoot.getRandomNumber(5) will choose a random number between 0 and 4, the ==0 will check if the random number is equal to 0. setLocation(getX()+5, getY()); this will move the character 5 pixels to the right if the random number was equal to 0
Blakehammond Blakehammond

2013/3/12

#
Thanks for your help. doing it now.
Blakehammond Blakehammond

2013/3/12

#
I get this message java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getOneIntersectingObject(Actor.java:912) at Dom.<init>(Dom.java:55) at Cave_background.prepare(Cave_background.java:357) at Cave_background.<init>(Cave_background.java:21)
Blakehammond Blakehammond

2013/3/12

#
I have put both characters in my stage
Game/maniac Game/maniac

2013/3/12

#
Show me the code for both of the characters
Blakehammond Blakehammond

2013/3/12

#
This is the enemy import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Drone here. * * @author (your name) * @version (a version number or a date) */ public class Drone extends Actor { /** * Act - do whatever the Drone wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(Greenfoot.getRandomNumber(5)==0) { setLocation(getX()+5, getY()); } } { Actor actor = getOneIntersectingObject(Drone.class); if(actor != null) { getWorld().removeObject(this); } } }
Blakehammond Blakehammond

2013/3/12

#
This is my player import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Dom here. * * @author (your name) * @version (a version number or a date) */ public class Dom extends Mover { private int speed=5; public Dom() { } public void act() { checkKeys(); } private void checkKeys(){ if(Greenfoot.isKeyDown("left")){ moveLeft(); } if(Greenfoot.isKeyDown("right")){ moveRight(); } if(Greenfoot.isKeyDown("up")){ moveUp(); } if(Greenfoot.isKeyDown("down")){ moveDown(); } } public void moveRight(){ setLocation(getX()+speed,getY()); } public void moveLeft(){ setLocation(getX()-speed,getY()); } public void moveUp(){ setLocation(getX(),getY() -speed); } public void moveDown(){ setLocation(getX(),getY() +speed); } { Actor actor = getOneIntersectingObject(Dom.class); if(actor != null) { getWorld().removeObject(this); } } }
There are more replies on the next page.
1
2