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

2019/9/18

Image Constantly Blinks/Changes

m0nday_ m0nday_

2019/9/18

#
Hey, So I've been working on a school project in which we are supposed to modify a game. We're using this game as a base. I want to make it so you can only shoot a certain type of alien (right now, I am using the soldier image and alien image as placeholders). You're only supposed to shoot the soldier, and therefore the soldier is supposed to be the image. The problem is that each alien constantly switched between soldier and alien in a blinking manner, and I have no idea why. Here's the code for the alien:
import greenfoot.*;
  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Alien extends Actor
{
    int SPEED = -7; // Speed of 10 in left direction "-"
    
    public Alien() {
    }

    public void act() {
       move (SPEED);

       Actor Bullet = getOneIntersectingObject(Bullet.class); 
       boolean isSeen;
       isSeen = true;
       int tempType = (int)((Math.random() * 3) + 1);
       
       if (isSeen = true) {
           if (tempType == 2) {
               setImage("Soldier.png");
               isSeen = false;
           }else {
               setImage("Alien.png");
               isSeen = false;
           }
        
       }
       
       // If alien touches left side of screen - Game Over
       if (getX()<0) {
           Greenfoot.playSound("game_over.mp3");
           GameOver gameover = new GameOver();
           getWorld().addObject(gameover, getWorld().getWidth()/2, getWorld().getHeight()/2);
           Greenfoot.stop();
       }
        // If Bullet touches alien - Game Over
        
       if (Bullet !=null) {
          if (tempType == 2){
              Greenfoot.playSound("explosion.wav");
              getWorld(). removeObject(Bullet);

              ((SpaceLand)(getWorld())).score.add(50);

              getWorld().removeObject(this); //remove alien from screen
              isSeen = false;
          }
          else {
              Greenfoot.playSound("game_over.mp3");
              GameOver gameover = new GameOver();
              getWorld().addObject(gameover, getWorld().getWidth()/2, getWorld().getHeight()/2);
              Greenfoot.stop();
              isSeen = false;
          }
       }
        
        
    }
}
Please help, if you can. Thanks. -D
danpost danpost

2019/9/18

#
Line 20 will always be executed because isSeen is being assigned a true value in line 19. That is, line 19 is equivalent to:
isSeen = true;
]if (true) ...
To compare values, use the conditional equality operator, '=='. As:
if (isSeen == true)
// or just this (since isSeen is a boolean)
if (isSeen)
You need to login to post a reply.