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

2016/9/19

Help With Method

1
2
AAkira AAkira

2016/9/19

#
So im still new with this thing, basically i have a class named Main,Enemy,MainBullet,SprayBullet and SprayBullet2. Main class is shooting with MainBullet and Enemy class shooting with SprayBullet. if the MainBullet touched with SprayBullet, the SprayBullet will dissappear and SprayBullet2 appear. they all in same extend class (Mover), so i made a method that if those bullet atWorldEdge it will dissappear/removed. but then comes the error 1.when SprayBullet didnt get hit by MainBullet, it will come to WorldEdge and dissappear but the function to calling SprayBullet2 still there 2.SprayBullet cant disappear when touched by MainBullet Code for SprayBullet
public void act() 
    
    {
        edge();
        move(5);
        setRotation(90);
        if (getX() < 10 || getX() != getWorld().getWidth() - 10) //Checking if SprayBullet still in MyWorld  
        {
            if (getY() < 10 || getY() != getWorld().getHeight() - 10)//Checking if SprayBullet still in MyWorld 
            {
               if(isTouching(MainBullet.class)) 
               {
                removeTouching(SprayBullet.class);
                SprayBullet2 a = new SprayBullet2();
                SprayBullet2 b = new SprayBullet2();
                SprayBullet2 c = new SprayBullet2();
                SprayBullet2 d = new SprayBullet2();
                SprayBullet2 e = new SprayBullet2();
                getWorld().addObject(a, this.getX(), 310);
                getWorld().addObject(b, this.getX(), 310);
                getWorld().addObject(c, this.getX(), 310);
                getWorld().addObject(d, this.getX(), 310);
                getWorld().addObject(e, this.getX(), 310);
                a.setRotation(315);
                b.setRotation(45);
                c.setRotation(90);
                d.setRotation(135);
                e.setRotation(225);
               }
            }
        }
    }
Code for edge ()
public void edge() {
     if(atWorldEdge()) 
     { 
         getWorld().removeObject(this);
     }
    }
Any solution for this 2 problem ?
danpost danpost

2016/9/20

#
The SprayBullet object will not intersect another SprayBullet object, which is what line 13 would remove. If you want to replace 'this' Spraybullet object with the SprayBullet2 objects that you are placing in the world, then use:
getWorld().removeObject(this);
Place this line somewhere between line 23 and 29 and remove line 13. As far as the other issue, it seem a bit much to call the 'edge' method and then use the coordinate checking to make sure the actor is in the world. You can simply remove the checks and insert the following at line 7:
if (getWorld() == null) return;
This can then be immediately followed by lines 14 on (unbracketed).
AAkira AAkira

2016/9/20

#
Ok, so now the SprayBullet when hitted by MainBullet already disappear, but when SprayBullet didnt get hit by MainBullet and come to WorldEdge it still have error my code line for SprayBullet now
 public void act() 
    {
        
        move(5);
        setRotation(90);
        edge();
         spray();
    }
    public void spray () 
 
    { 
       Actor spray = getOneIntersectingObject(MainBullet.class);
        if(spray !=null) 
            {
               SprayBullet2 a = new SprayBullet2();
               SprayBullet2 b = new SprayBullet2();
               SprayBullet2 c = new SprayBullet2();
               SprayBullet2 d = new SprayBullet2();
               SprayBullet2 e = new SprayBullet2();
               getWorld().addObject(a, this.getX(), 310);
               getWorld().addObject(b, this.getX(), 310);
               getWorld().addObject(c, this.getX(), 310);
               getWorld().addObject(d, this.getX(), 310);
               getWorld().addObject(e, this.getX(), 310);
               getWorld().removeObject(this);
               a.setRotation(315);
               b.setRotation(45);
               c.setRotation(90);
               d.setRotation(135);
               e.setRotation(225);
            }
         if (getWorld() == null)
            {
                return;
            }

    }
is that right place for getWorld() == null ?
danpost danpost

2016/9/20

#
AAkira wrote...
< Code Omitted > is that right place for getWorld() == null ?
No. It needs to be right after the call to 'edge' in the act method. The reason for this is so no method calls that require the actor be in the world are called when the actor is not in the world. The line forces an exit out of the act method if the actor is no longer in the world.
AAkira AAkira

2016/9/20

#
Great, now it works perfectly ! Thank you danpost
AAkira AAkira

2016/9/20

#
I have a question again, i add EnemyBullet to Mover class and i want it to make upside-down-triangle pattern ( \ / ) i place the code in Enemy class
private void fire() 
    
    { //Adding enemy bullet
        int timer = 4;
        if (timer>=3)
        {
         timer--;
         EnemyBullet a = new EnemyBullet();
         EnemyBullet b = new EnemyBullet();
         getWorld().addObject(a, getX()-40, getY());
         getWorld().addObject(b, getX()+40, getY());
         if (timer>=2)
         
         {
          timer--;   
          EnemyBullet c = new EnemyBullet();
          EnemyBullet d = new EnemyBullet();
          getWorld().addObject(c, a.getX()-60, a.getY()-15);
          getWorld().addObject(d, b.getX()+60, b.getY()-15);
         }
        }
    }
the bullet appear perfectly when c & d (EnemyBullet) Y position -15, but when i change the value to -16 and above that, the bullet didnt appear, can you tell me which part is wrong ?
danpost danpost

2016/9/20

#
AAkira wrote...
< Code Omitted > the bullet appear perfectly when c & d (EnemyBullet) Y position -15, but when i change the value to -16 and above that, the bullet didnt appear, can you tell me which part is wrong ?
There is not enough information given to determine why you are getting that behavior. The entire Enemy class code plus that of the EmptyBullet class may be necessary to figure this one out. However, right now I can say that line 12 through 14 and line 20 could be remove without changing anything. That is, the condition will always be true because it is only checked when the condition on line 5 is true and line 7 ensures that the condition on line 12 will always be true when checked.
AAkira AAkira

2016/9/20

#
code for Enemy class
public class Enemy extends Object
{
    public void act() //Enemy act
    
    {
        move(5);
        if(getX() < 5 || getX() > getWorld().getWidth() - 5) //Checking enemy if at world edge
        {
            turn(180);
            getImage().mirrorVertically();
        }
        collision();        
        Spray();
        if(Greenfoot.getRandomNumber(200)<5) //Counter for random bullet and adding it
        {
            RandomBullet bullet = new RandomBullet();
            getWorld().addObject(bullet, getX(), getY());
        }
        if(Greenfoot.getRandomNumber(200)<5) //Counter for health item and adding it
        {
            HealthItem health = new HealthItem();
            getWorld().addObject(health, Greenfoot.getRandomNumber(460), Greenfoot.getRandomNumber(20));
        }
        if(Greenfoot.getRandomNumber(200)==50)
        {
            fire();
        }
    }
    int score = 0; //Initial score 
    public void collision()  { //Collision with main bullet
        if(isTouching(MainBullet.class))
            {
                score ++;  
            }
        getWorld().showText("Score : " + score, 400, 20);
     }
     
    private void fire() 
    
    
    { //Adding enemy bullet
        move(0);
        this.setLocation(230,20);
        int timer = 4;
        if (timer>=3)
        
        {
         timer--;
         EnemyBullet a = new EnemyBullet();
         EnemyBullet b = new EnemyBullet();
         getWorld().addObject(a, getX()-40, getY());
         getWorld().addObject(b, getX()+40, getY());
         if (timer>=2)
         {
          timer--;   
          EnemyBullet c = new EnemyBullet();
          EnemyBullet d = new EnemyBullet();
          getWorld().addObject(c, a.getX()-40, a.getY()-16);
          getWorld().addObject(d, b.getX()+40, b.getY()-16);
          /*if (timer>=1)
          {
           timer--;   
           EnemyBullet e = new EnemyBullet();
           EnemyBullet f = new EnemyBullet();
           getWorld().addObject(e, getX()-120, getY()-40);
           getWorld().addObject(f, getX()+120, getY()+40);
          }
          if (timer>=0)
          {
          EnemyBullet g = new EnemyBullet();
          EnemyBullet h = new EnemyBullet();
          getWorld().addObject(g, getX()-160, getY()-60);
          getWorld().addObject(h, getX()+160, getY()+60);
          }*/
         }
        }
    }
    SprayBullet bullet = new SprayBullet();
    public void Spray() {
        if(Greenfoot.getRandomNumber(500)<5) //Counter for spray bullet and adding it
        {
            getWorld().addObject(bullet, getX(), getY());
        }
    }
}
as you can see im still commenting line after c & d Enemy Bullet and code for EnemyBullet only this
    public void act() 
    {
        setRotation(90);
        move(5);
        edge();
    }
image when i set Y to -15 image when i set Y to -16 and above
AAkira AAkira

2016/9/20

#
it seems cant attach picture link for when i set Y to -15 and below -15 link for when i set Y to -16 and above -16
danpost danpost

2016/9/21

#
This should show them (I went to properties of the image and got the file names): link for when i set Y to -15 and below link for when i set Y to -16 and above
danpost danpost

2016/9/21

#
I wonder why the images are of different sizes. It could be that all four are there -- but, outside the view window. What is the 'super' call in your world constructor? is the world unbounded ('false' as the last argument in the 'super' call)? The background images are definitely not the same. I am guessing that this might have something to do with why you only see two of them.
danpost danpost

2016/9/21

#
I see that you have the Enemy class extend the Object class; but, you are using Actor class methods within it. Do you have another class called Object that extends Actor, that the Enemy class extends? If so, rename that Object class to something else. All classes already extend an Object class (which is the root class of ALL classes -- see Object class API documentation.
AAkira AAkira

2016/9/22

#
Sorry for late replying, my internet connection went down since yesterday By the way, i'm already figure this out and already rename the class, thanks again for replying Anyway, i want to ask, how to call variable from actor class to world class ? In this case, when MainBullet hit Enemy, it will + the score (i'm making new class for score point so you can ignore the code above). in MainWorld (when the game is played), the score is already increase when get hitted, but when i'm trying to get variable from Score class to LoserWorld class it show 0 (basically making game over screen while showing your score) code for Score class
import greenfoot.*;  
import java.awt.Color;

public class Score extends Actor
{

    int score = 0; //Initial score
    public Score() {
        update();
    }
    
    public void act() 
    {
        update();
    }
    
    public void hitscore() 
    {
        score+=10;
    }
    
    public void scoreplus() {
        score += Greenfoot.getRandomNumber(100);
    }
    
    public void update() {
        setImage(new GreenfootImage("Score: " + score, 30, Color.WHITE, Color.BLACK));
    }
    

}
code for LoserWorld
public class LoserWorld extends World
{

   
    public LoserWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(429, 620, 1);
        prepare();
    }
    Score point = this.getScore();;
    int a = point.score;
    public void prepare() {
        showText("Score : " + a,200,200);
    }
    
     public Score getScore() {
        return point;
    }
Trying that but score in LoserWorld still stuck in 0
Super_Hippo Super_Hippo

2016/9/22

#
Well, it can't work. Look at line 11 of the LoserWorld class. 'point' ends up being 'null' because the 'getScore' method returns 'point' which this line just created. It has nothing to do with the Score object which is in the MainWorld. To get the score value in the LoserWorld, you need to pass the variable, the Score object or the MainWorld to the LoserWorld. (Probably easiest to just pass the value.) So line 5 in the LoserWorld class will be this:
public LoserWorld(int score)
When creating the world, you pass the score from the MainWorld and you can use this 'score' in the LoserWorld constructor then.
AAkira AAkira

2016/9/22

#
Super_Hippo wrote...
Well, it can't work. Look at line 11 of the LoserWorld class. 'point' ends up being 'null' because the 'getScore' method returns 'point' which this line just created. It has nothing to do with the Score object which is in the MainWorld. To get the score value in the LoserWorld, you need to pass the variable, the Score object or the MainWorld to the LoserWorld. (Probably easiest to just pass the value.) So line 5 in the LoserWorld class will be this:
public LoserWorld(int score)
When creating the world, you pass the score from the MainWorld and you can use this 'score' in the LoserWorld constructor then.
is there any line should i remove ? because it comes up with syntax error
There are more replies on the next page.
1
2