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

2013/1/19

HELP PLZ my project is due on monday!!

1
2
SpritesKing SpritesKing

2013/1/19

#
i need help on when my sprite character does the attack animation in which is "space". i want the enemies to disappear AND add score. i can make them disappear without losing health but i can never make the score work ~ i want to cry. heres my code for the animation when i pressed space.
 public void attack() 
    {

        if (face==0)
        {

            if(frames == 0) {setImage("linkr.PNG");}
            else if(frames == 1) {setImage("linkr2.PNG");}
            else if(frames == 2) {setImage("linkr3.PNG");}
            else if(frames == 3) {setImage("linkr4.PNG");}
            else if(frames == 4) {setImage("linkr5.PNG");}
            else if(frames == 5) {setImage("linkr6.PNG");}
            else if(frames == 6) {

                frames = 0;
                return;
            }
            frames++;

        }

        if (face==1)
        {

            if(frames == 0) {setImage("linkr.PNG");}
            else if(frames == 1) {setImage("linkr2.PNG");}
            else if(frames == 2) {setImage("linkr3.PNG");}
            else if(frames == 3) {setImage("linkr4.PNG");}
            else if(frames == 4) {setImage("linkr5.PNG");}
            else if(frames == 5) {setImage("linkr6.PNG");}
            else if(frames == 6) {

                frames = 0;
                return;
            }
            frames++;
            mirror();
        }
        if (face==2)
        {

            if(frames == 0) {setImage("linku1.png");}
            else if(frames == 1) {setImage("linku2.png");}
            else if(frames == 2) {setImage("linku3.png");}
            else if(frames == 3) {setImage("linku4.png");}
            else if(frames == 4) {setImage("linku5.png");}
            else if(frames == 5) {setImage("linku6.png");}
            else if(frames == 6) {
                setImage("linku1.png");
                frames = 0;
                return;
            }
            frames++;

        }
        if (face==3)
        {

            if(frames == 0) {setImage("linkd1.png");}
            else if(frames == 1) {setImage("linkd2.png");}
            else if(frames == 2) {setImage("linkd3.png");}
            else if(frames == 3) {setImage("linkd4.png");}
            else if(frames == 4) {setImage("linkd5.png");}
            else if(frames == 5) {setImage("linkd6.png");}
            else if(frames == 6) {
                setImage("linkd1.png");
                frames = 0;
                return;
            }
            frames++;

        }
        if (attackFramesLeft == 7)
        {
            music1 = new GreenfootSound("sword1.wav");
            music1.play();
        }
        attackFramesLeft--;

    }
SpritesKing SpritesKing

2013/1/19

#
press space* and heres my enemie code
    public void checkIfHit()
    {
        normal normal =(normal)getOneIntersectingObject(normal.class);

        if(normal!=null  )
        {
            if( normal.checkForAttack())
                ((world)getWorld()).subtractHealth(5);
            music.play();
            removeMe = true;
        }
    }
check for attack is for when i hit space from the normal and heres the code for it
    boolean hit = false;
    public boolean checkForAttack()
    {
        if (Greenfoot.isKeyDown("space"))
        {
            hit = false;
            return hit;  

        }
        else 
        {
            hit = true;
            return hit;
        }

    }
danpost danpost

2013/1/19

#
You need to show the code around which you make the enemy disappear.
SpritesKing SpritesKing

2013/1/19

#
thanks for the reply i just updated my scenario if you dont mind checking it out since it has everything you will need to see. greatly appreciated
danpost danpost

2013/1/19

#
It might be best if you move the collision checking (between the normal and safes) to the normal class. I think the safes are removing themselves before the normal realizes there was a collision..
SpritesKing SpritesKing

2013/1/19

#
i know this might be a huge favor but can you please code the score part for me?
vonmeth vonmeth

2013/1/19

#
There are a number of things wrong. The way you handle the attack frames can be fixed in a simpler way. A way that will lend towards easier checks later on.
    private boolean attacking; // add to the top

        if("space".equals(key) && attacking == false)
        {
            attacking = true;
            music1 = new GreenfootSound("sword1.wav");
            music1.play();
        }
        // rest of your code
        attack(); // add to your act method outside of any ifs
        score(); // this wasn't in your act method, it was in your constructor, but this wasn't what was causing the problem
You then need to change your attack method. What we will do is check first if attacking is true, then at the 'if' for frames == 6, we will add attacking = false.
    public void attack() 
    {
        if(attacking){
            if (face==0)
            {

                if(frames == 0) {setImage("linkr.PNG");}
                else if(frames == 1) {setImage("linkr2.PNG");}
                else if(frames == 2) {setImage("linkr3.PNG");}
                else if(frames == 3) {setImage("linkr4.PNG");}
                else if(frames == 4) {setImage("linkr5.PNG");}
                else if(frames == 5) {setImage("linkr6.PNG");}
                else if(frames == 6) {
                    attacking = false;
                    frames = 0;
                    return;
                }
                frames++;

            }

            if (face==1)
            {

                if(frames == 0) {setImage("linkr.PNG");}
                else if(frames == 1) {setImage("linkr2.PNG");}
                else if(frames == 2) {setImage("linkr3.PNG");}
                else if(frames == 3) {setImage("linkr4.PNG");}
                else if(frames == 4) {setImage("linkr5.PNG");}
                else if(frames == 5) {setImage("linkr6.PNG");}
                else if(frames == 6) {
                    attacking = false;
                    frames = 0;
                    return;
                }
                frames++;
                mirror();
            }
            if (face==2)
            {

                if(frames == 0) {setImage("linku1.png");}
                else if(frames == 1) {setImage("linku2.png");}
                else if(frames == 2) {setImage("linku3.png");}
                else if(frames == 3) {setImage("linku4.png");}
                else if(frames == 4) {setImage("linku5.png");}
                else if(frames == 5) {setImage("linku6.png");}
                else if(frames == 6) {
                    attacking = false;
                    setImage("linku1.png");
                    frames = 0;
                    return;
                }
                frames++;

            }
            if (face==3)
            {

                if(frames == 0) {setImage("linkd1.png");}
                else if(frames == 1) {setImage("linkd2.png");}
                else if(frames == 2) {setImage("linkd3.png");}
                else if(frames == 3) {setImage("linkd4.png");}
                else if(frames == 4) {setImage("linkd5.png");}
                else if(frames == 5) {setImage("linkd6.png");}
                else if(frames == 6) {
                    attacking = false;
                    setImage("linkd1.png");
                    frames = 0;
                    return;
                }
                frames++;

            }
        }
    }
We have now fixed you attack animation and added a very easy way to check if you are currently attacking or not. You next need to do a number of things. Remove your checkIfHit method in your safe classes. This is important, we want the normal class to be doing all the work here. Remove your methods in you attack class (we don't need them). You can remove the other methods you added to your 'normal' class for the scoring and such as well. You just need one method for it, and we will be altering your score method to handle everything.
    public void score()
    {
        Actor enemy = getOneIntersectingObject(safe.class);

        if(enemy!=null)
        {
            if(attacking){
                counter.add(5);
                music2 = new GreenfootSound("sword2.wav");
                music2.play();
                ((world)getWorld()).removeObject(enemy);
            } else {
                GreenfootSound music = new GreenfootSound("hurt.wav");
                music.play();
                ((world)getWorld()).subtractHealth(5);
                ((world)getWorld()).removeObject(enemy);
            }
        }
        enemy = getOneIntersectingObject(safe2.class);

        if(enemy!=null)
        {
            if(attacking){
                counter.add(5);
                music3 = new GreenfootSound("sword3.wav");
                music3.play();
                ((world)getWorld()).removeObject(enemy);
            } else {
                GreenfootSound music = new GreenfootSound("hurt.wav");
                music.play();
                ((world)getWorld()).subtractHealth(5);
                ((world)getWorld()).removeObject(enemy);
            }
        }
    }
danpost danpost

2013/1/19

#
OK, here goes:
// in the 'safe' class

// remove the 'checkIfHit' method

// change 'act' method to
public void act()
{
    moveRight();
    outOfMap();
}

// change constructor to
public safe()
{
}

// remove the 'removeMe' field declaration
boolean removeMe; // remove this line

// ***************************************

// in the 'safe2' class
// do exactly the same as what you did in the 'safe' class above

// ***************************************

// in your 'normal' class

// remove the constructor thats starts with
public normal()

// make the following the first line in the 'act' method
if (checkForAttack()) score(); else checkHit();

// add the following method to the class
public void checkHit()
{
    world w=(world)getWorld();
    if(canSee(safe.class))
    {
        w.subtractHealth(5);
        w.removeObject(getOneObjectAtOffset(0, 0, safe.class));
    }
    if(canSee(safe2.class))
    {
        w.subtractHealth(5);
        w.removeObject(getOneObjectAtOffset(0, 0, safe2.class));
    }
}

// remove the line located before your 'checkForAttack' method
boolean hit = false; // remove this line

// change the 'checkForAttack' method to
public boolean checkForAttack()
{
    return Greenfoot.isKeyDown("space");
}


// change the 'score' method to
public void score()
{
    attack();
    if(canSee(safe.class))
    {
        attack(safe.class);
        counter.add(5);
        music2 = new GreenfootSound("sword2.wav");
        music2.play();
    }
    if(canSee(safe2.class))
    {
        attack(safe2.class);
        counter.add(5);
        music3 = new GreenfootSound("sword3.wav");
        music3.play();
    }
}

// remove the following from the 'act' method
if("space".equals(key))
{
    attackFramesLeft = 7;
}
if(attackFramesLeft > 0)
{
    attack();
}

// copy the 'canSee' and 'attack' methods from the attack class and paste them in your normal class

// remove the attack class from the scenario

// change the normal class declaration to
public class normal extends Actor


// change the 'attack' method to (the one copied from the attack class)
public void attack(Class clss)
{
    getWorld().removeObject(getOneObjectAtOffset(0, 0, clss));
}
SpritesKing SpritesKing

2013/1/19

#
i tried vometh's method and i am not sure if it works because i will have a null pointer error for "counter.add(5)" :(
SpritesKing SpritesKing

2013/1/19

#
danpost" i need the attackframeleft due to the animation process with only one hit of space thanks you both for your time :) greatly appreciated. i now will try and look at the error and start doing "when the score reaches 300, different kind of enemies will spawn" OR "the enemies will move faster as the score goes up" and i will need a starting and ending screen that includes instruction, and start game.
danpost danpost

2013/1/19

#
The thing about it is, 'getKey' is triggered when a key is released. The key must be pressed first and the animation triggered by 'isKeyDown' will begin many cycles before the key is released. But I guess you could still use it to reset your frame counter.
vonmeth vonmeth

2013/1/19

#
Not sure why you are getting an exception .... It worked fine for me.
SpritesKing SpritesKing

2013/1/19

#
vonmeth sorry then it must be me.
vonmeth vonmeth

2013/1/19

#
Hah, no need to be sorry. I mean, it is in your normal class, right? Your normal class has a reference to your counter, so it shouldn't be null.
SpritesKing SpritesKing

2013/1/19

#
do you mind check it out for me? i just updated my scenario thanks you !
There are more replies on the next page.
1
2