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

2015/12/12

how to spawn actor?

1
2
3
Blubberfish007 Blubberfish007

2015/12/14

#
thanks everyone again :))
Blubberfish007 Blubberfish007

2015/12/14

#
sorry, but how could i remove the enemy if the hero touches it
Blubberfish007 Blubberfish007

2015/12/14

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public void attack()
    {
        if (Greenfoot.isKeyDown("space"))
        {
          if(Touching == false)
          {
              if (isTouching())
            {
              remove();
              setLocation(401,301);
              Touching = true;
            }
            else
            {
              move(1);
              Touching = false
               
            }
          }
        }
    }
this is what ive done, the parts i can't do are the bits where it says is touching() and remove()
Blubberfish007 Blubberfish007

2015/12/14

#
i want to make it so that when you eat one and get moved back, you don't go forward again if you hold the space bar for a fraction too long, and that you have to let go of space and hold it again to move
danpost danpost

2015/12/14

#
I get the feeling that you overcomplicated things here. You can use the state of the key to determine whether to move or remove, meaning that the 'Touching' field is not needed at all:
1
2
3
4
5
6
7
8
9
10
11
12
if (Greenfoot.isKeyDown("space"))
{
    if (isTouching(Enemy.class))
    {
        removeTouching(Enemy.class);
        setLocation(401, 301);
    }
}
else
{
    move(1);
}
Super_Hippo Super_Hippo

2015/12/14

#
By the way, you can 'edit' your last post instead of creating multiple posts.
Blubberfish007 Blubberfish007

2015/12/14

#
is there a way to make it so that the player, after eating an enemy, and gets moved back, it stays there rather than moving forward a bit after if you hold it down for a bit too long? because if not i can just put it so that i can press a key to make it go to the middle
danpost danpost

2015/12/14

#
With the code I supplied, your player will not move until the key is released.
Blubberfish007 Blubberfish007

2015/12/15

#
sorry, i must have mislead you, is there a way i could have it move when the space key is down, but after it is move to the middle, it wont move unless the key is released and pressed again?
danpost danpost

2015/12/15

#
I think you will need two boolean fields, then -- one for the state of the key and the other for the state of waiting for keypress (or, in a holding pattern):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// instance fields
boolean spaceDown, spacePressed;
 
// act code
if (Greenfoot.isKeyDown("space") != spaceDown)
{
    spaceDown = ! spaceDown;
    if (spaceDown && ! spacePressed)
    {
        spacePressed = true;
    }
}
if (spaceDown && spacePressed)
{
    move(1);
    if (isTouching(Enemy.class))
    {
        removeTouching(Enemy.class);
        setLocation(401, 301);
        spacePressed = false;
    }
}
Blubberfish007 Blubberfish007

2015/12/15

#
it worked thanks:)
You need to login to post a reply.
1
2
3