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

2015/3/24

Colliding

1
2
3
4
aliozkan aliozkan

2015/3/24

#
In my game I want to assign random numbers to an enemy and a main object i.e. enemy = 10 main= 20 and I also want to see these numbers above their heads like 10 units up of the objects on the screen. And when two objects collide, if main object has bigger amount of points then I want to add the half of the points that the enemy object has, to the main object, but if enemy has bigger amount of points then I want to end the game. Can anyone help me please I couldn't figure out how to do that.
danpost danpost

2015/3/24

#
What part are you having trouble with? -- getting the numbers to appear above the actors? -- getting the numbers to stay above the actors? -- getting the values of those numbers to change? -- getting the values to change properly?
0gener 0gener

2015/3/24

#
Well first you have to define the the point of each object first:
1
2
3
4
private int enemyP = Greenfoot.getRandomNumber(50);
 
private int mainP = Greenfoot.getRandomNumber(50);
// 50 is just the limit of point they can have... in this case the number of points will go from 0 to 49.
Then you need a method that returns the number of points of your enemy in your enemy class:
1
2
3
4
public int getEnemyP()
{
      return enemyP;
}
Then inside your main class create a method that compares the points and do what you want:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public void collision()
{
      int enemyP = enemy.getEnemyP();
      if(isTouching(enemy.class))
      {
             if(mainP >= enemyP)
             {
                     mainP = mainP + enemyP / 2;
             }
             else
             {
                     Greenfoot.pause();
             }
      }
}
I think everything is right... If nyone sees a mistake warn him please.
danpost danpost

2015/3/24

#
@0gener, in line 3 of the 'collision' method, you are using 'enemy' as a variable name; however, it is not defined within the method. If you are using it as a class name, then it is still not defined as the field is an instance field in the class -- not a class field.
0gener 0gener

2015/3/24

#
Yes you are right. He can define then the enemyP inside the main class (since its just a random number), this way he could just do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public void collision()
{
     // int enemyP = enemy.getEnemyP();  dont need because enemyP is declared inside main.
      if(isTouching(enemy.class))
      {
             if(mainP >= enemyP)
             {
                     mainP = mainP + enemyP / 2;
             }
             else
             {
                     Greenfoot.pause();
             }
      }
}
Super_Hippo Super_Hippo

2015/3/24

#
I don't think that this is a good idea. If there is more than one enemy (which probably will be the case), you can't do it like this. Instead, you should get a reference to the object and check its number.
aliozkan aliozkan

2015/3/25

#
Danpost, I am having trouble in getting the numbers to appear And also getting those values change.
danpost danpost

2015/3/25

#
aliozkan wrote...
Danpost, I am having trouble in getting the numbers to appear And also getting those values change.
What have you tried?
aliozkan aliozkan

2015/3/25

#
I don' know the method for making those random numbers appear so I couldn't try anything.It's been few weeks since I started to greenfoot that's why I don't know much things, if you could teach me how to do it, it would be great
aliozkan aliozkan

2015/3/25

#
But I can make them stay above the actors if I make them appear
danpost danpost

2015/3/25

#
Take a look at the following complete class code:
1
public class BasicActor extends greenfoot.Actor {}
I realize that this may seem quite strange at first; but, let me explain the beauty of such a class. Let us say you create an instance (an Actor object) from this class:
1
Actor basic = new BasicActor();
Now, there is quite a bit you can do with an actor like this. It can, in essence, replace ALL of the following type classes at once: (1) a Text class; (2) a Score class; (3) a Lives class; (4) a Message class; (5) a Health class; (6) a Button class; and more. Because of its lack of coding, it is not bound to the guidelines of what you code it for. Adding state and behavior to the objects created from the class is not difficult; but, trying to remove state and behavior that is already coded into the class would be impossible. Each actor that will have a number above it should create one object from the class and keep a reference to that object:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// instance fields in player and enemy classes
private int randomValue = 1+Greenfoot.getRandomNumber(50);
private Actor randomValueDisplay = new BasicActor();
// utilize the 'addedToWorld(World)' method to add the actor into the world
protected void addedToWorld(World world)
{
    GreenfootImage image = new GreenfootImage(""+randomValue, 14, null, null);
    randomValueDisplay.setImage(image);
    world.addObject(randomValueDisplay, getX(), getY());
    updateRandomValueDisplayLocation();
}
// method to call at the end of the act method of the player and enemy
private void updateRandomValueDisplayLocation()
{
    randomValueDisplay.setLocation(getX(), getY()-30);
}
The first two lines in the 'addedToWorld' method that set the image of the BasicActor object could be placed in the constructor of the class(es). You should probably add the following method as well to the player and enemy classes:
1
2
3
4
5
public void remove()
{
    getWorld().removeObject(randomValueDisplay);
    getWorld().removeObject(this);
}
Then call that method instead of removing the object directly so that the BasicActor object that belong to the player or enemy is removed also when it is removed.
aliozkan aliozkan

2015/3/25

#
Thank you so much. It took me a while to figure it out but now it works perfectly. My other problem is how to make them compare the points when they collide could you please help me .
fejfo fejfo

2015/3/25

#
if danpost's method doesn't word ( what I will doubt) u can try to draw the score on the enmie.
1
2
3
4
5
6
7
8
9
protected void addedToWorld(World world) {
    int textSize = 30; //enter the size of the text here
    GreenfootImage baseImage = getImage();
    GreenfootImage emptyImage = new    GreenfootImage(baseImage.getWidth,baseImage.getHeight+textSize);
    GreenfootImage text = new GreenfootImage(""+<the warible for the enmies points>,textSize,null,null);
    emptyImage.drawImage(text,0,0);
    emptyImage.drawImage(baseImage,0,texSize);
    setImage(emptyImage);
}
aliozkan aliozkan

2015/3/25

#
thanks fejfo but danpost's method works I can see the points now.
Super_Hippo Super_Hippo

2015/3/25

#
Since randomValue (which I probably would name differently, maybe 'size' if it is meant to be a "large fish eat small fish"-game, but that doesn't matter) is a private field, you have to create a getter-method in the class of the Enemy (in the following this class is called 'Enemy', I don't know how you called it).
1
2
3
4
public int getRandomValue()
{
    return randomValue;
}
Then you can use something like this in the class of the user-controlled object:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Enemy e = (Enemy) getOneIntersectingObject(Enemy.class);
if (e != null)
{
    final int v = e.getRandomValue();
    if (randomValue > v)
    {
        randomValue += (v+1)/2;
        e.remove();
    }
    else /*if (randomValue < v)*/ //I don't know what should happen if both have the same size
    {
        remove(); //or not
        //end the game - e.g. 'Greenfoot.stop();'
    }
}
There are more replies on the next page.
1
2
3
4