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

2019/5/31

Enemy health

ButerWarior ButerWarior

2019/5/31

#
I've only started programming a week ago so I'm still pretty much a noob. rn I'm making a game similar to galaxy attack, where you shoot alien ships (how simple). I'm trying to make the aliens die after 3 shots of the laser, but I can't really figure how to do it. I've tried all the ideas that might work but they never did. Whenever the laser hits the alien, the laser disappears like I programmed it to but the aliens don't take damage. This is my code for the laser.
public void act()
        {
        setLocation(getX(), getY()-10);
        Actor monster;
        monster = getOneObjectAtOffset(0,-20, monsters.class);
        if (isAtEdge()|| monster!=null)
        {
            getWorld().removeObject(this);
        }
    }
and this is my code for the aliens.
public void collisionWithLaser()
    {
        hp = hp - 50;
        Greenfoot.delay(1);
    }
    public void act()
    
    {
        Actor laser;
        if (getOneObjectAtOffset(0,25,laser.class)!=null)
        {
            collisionWithLaser();
        }
        if (hp <= 0)
        {
            getWorld().removeObject(this);
        }
        }
danpost danpost

2019/5/31

#
Let the laser ignore the monster and have the monster remove any bullets that hit it.
ButerWarior ButerWarior

2019/5/31

#
uh when i did that it told me that laser is not a variable even though i had Actor laser.
danpost danpost

2019/5/31

#
ButerWarior wrote...
uh when i did that it told me that laser is not a variable even though i had Actor laser.
Cannot help without seeing what you tried. Show revised class codes.
ButerWarior ButerWarior

2019/6/1

#
int hp = 150;
    public void collisionWithLaser()
    {
        hp = hp - 50;
        Greenfoot.delay(1);
    }
    public void act()
    
    {
        Actor laser;
        if (getOneObjectAtOffset(0,25,laser.class)!=null)
        {
            collisionWithLaser();
            World world;
            world=getWorld();
            world.removeObject(laser);
        }
        if (hp <= 0)
        {
            getWorld().removeObject(this);
        }
        }
danpost danpost

2019/6/1

#
You do not assign anything to it. Line 10 declares the variable; but its value is null, meaning no object is assigned to it..
ButerWarior ButerWarior

2019/6/1

#
What do you mean by no object is assigned?
danpost danpost

2019/6/1

#
ButerWarior wrote...
What do you mean by no object is assigned?
Nowhere do you have:
laser = ...;
All line 10, as you have it, does is declare a new variable named laser that is allowed to hold an Actor object (that is, of type Actor) . Nowhere do you "set it equal to anything", "set a value to it" or, for an Object reference variable as this is, have it reference any possible Actor object.
ButerWarior ButerWarior

2019/6/1

#
ohhh that makes a lot more sense, now I'll try fixing it by myself. Thank you for the help!
ButerWarior ButerWarior

2019/6/1

#
I tried doing stuff and searched up other discussions but i still couldn't find how to do this. The other scenarios that I found just simply did what I did but their's worked. I'm pretty sure I'm missing out on something here, but I can't manage to find it.
danpost danpost

2019/6/1

#
I give you a big hint. What are you comparing to null in the if statement in the next line? Could it be a possible Actor (laser type) object?
ButerWarior ButerWarior

2019/6/1

#
I thought this code meant that I was comparing the null with laser based on the monster, or is that wrong?
danpost danpost

2019/6/1

#
ButerWarior wrote...
I thought this code meant that I was comparing the null with laser based on the monster, or is that wrong?
That is right -- if I understand you correctly. However, the laser that hits the monster is the one you want to remove from the world. To do that, you need a reference to that laser object, if it exists.
ButerWarior ButerWarior

2019/6/1

#
Thank you! Now I found out the problem, and I was able to fix it. Like you said, all I had to do was refer to the laser that hit the monster by adding a variable to the code. Now it works, and the bullet would dissappear, while the monsters were removed after 3 hits!
You need to login to post a reply.