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

2018/1/20

Change direction of an Actor

1
2
L2plex L2plex

2018/1/20

#
Hello I'm looking for a smart way to turn my snowballs into the right direction. I use them to destroy the enemy.... The direction depends on the direction of another actor. I found some sultions, where I have to name the snowballs, but because of the fact that there can be multiple Snowballs in the game at the same time, it would make it way more complicatet to give them a name when I create them (I guess...) I had a simple idea, but I dont know why it doesn't work. This method is called in the act-method in the Snowball-Class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public void direction(){
        if (singleAct == 0){
           if (Greenfoot.isKeyDown("a")){              // Left
                setRotation(180);       
           }
           else
           if (Greenfoot.isKeyDown("d")){              // Right
                setRotation(0);      
           }
           else
           if (Greenfoot.isKeyDown("w")){              // Up
                setRotation(-90);     
           }
           else
           if (Greenfoot.isKeyDown("s")){              // Down
                setRotation(90);
           }   
           System.out.println(singleAct);
           singleAct = 1;
        }           
    
Instead of the Method above, I think the best idea would be to work with the getRotation() and setRotation(), but I had issues with set them up right. Even the tutorial "How to access one object from another" couldn't help me. : ( My World is named Labyrinth, the class I want the rotation of is named Player (actor; player). The Class Snowball needs the access... Can someone pls help me with getting the Rotation of the player.
Super_Hippo Super_Hippo

2018/1/20

#
If you want to throw the snowball at the direction of the player when throwing it, you can use something like this when throwing one:
1
2
3
Actor snowball = new Snowball();
getWorld().addObject(snowball, getX(), getY());
snowball.setRotation(getRotation());
And then, the snowball should only move straight and should not change its direction.
L2plex L2plex

2018/1/20

#
Thanks Hippo I my case I have more than one (max 4) snowball at the time. So if I name them snowball1, snowball2, etc. I would need to check every time I wanna place a snowball into the world, which of these 4 aren't in the world. Isn't it possible to call in the Snowball Class for the rotation of the player? (I'm not sure if this sentens makes sense with the prepositions I used... ^^')
Super_Hippo Super_Hippo

2018/1/20

#
No, you don't need to name them. You use these three lines I gave you when you create one snowball. You do not have the first outside the method. If you use all three like this together, you can create infinite snowballs. (Well, almost infinite).
CxVercility CxVercility

2018/1/20

#
L2plex wrote...
Thanks Hippo I my case I have more than one (max 4) snowball at the time. So if I name them snowball1, snowball2, etc. I would need to check every time I wanna place a snowball into the world, which of these 4 aren't in the world. Isn't it possible to call in the Snowball Class for the rotation of the player? (I'm not sure if this sentens makes sense with the prepositions I used... ^^')
Of course it is (Actor.getRotation() ) but its not necessary as Super_Hippo has already explained. You can set their rotation right after creating them in the Actors class
L2plex L2plex

2018/1/20

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public void throwBall(){
        if (Greenfoot.isKeyDown("space") && throwBall == 0){          
           int nSnowballs = getWorld().getObjects(Snowball.class).size();      
           if (nSnowballs<4){          
                Actor snowball = new Snowball();
                getWorld().addObject(snowball, getX(), getY());
                snowball.setRotation(getRotation());
            }
           throwBall = 1;         
        }
        else
        if (! Greenfoot.isKeyDown("space")){           
           throwBall = 0;          
        }   
    }
L2plex L2plex

2018/1/20

#
I changed the method in the Player class, where I create the snowballs, but I doesn't really work. The snowballs go all the same direction, as before....
Super_Hippo Super_Hippo

2018/1/20

#
Show the Snowball class.
CxVercility CxVercility

2018/1/20

#
Firstly, throwball should be a boolean if you use it to determine whether a ball is being thrown or not. Try this.getRotation() instead of just getRotation()
L2plex L2plex

2018/1/20

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Snowball here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Snowball extends Actor
{
    int counter = 0;
    int isDead = 0;
     
    public Snowball()
    {
       GreenfootImage image = getImage();
       image.scale(10, 10);
       setImage(image);
       setRotation(-90);      
        
    }   
     
    /**
     * Act - do whatever the Snowball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
 
        
       move();      
       collisionCheck();
       setRotation(-90);
       hitEnemy();
        
       isDead();
    }
       
    public void move(){
        if (counter < 180){
            move(3);
            counter++;
        }
        else
        if(counter == 180){
            isDead = 1;
             
        }   
        counter++;
    }
     
    public void collisionCheck(){              
        Actor localBlock = getOneIntersectingObject(Block.class);               
        if(localBlock != null){
            isDead = 1;           
        }           
    }
     
    public void hitEnemy(){
        Actor localEnemy = getOneIntersectingObject(Enemy.class);               
        if(localEnemy != null){
            getWorld().removeObject(localEnemy);
            isDead = 1;           
        }   
    }  
     
    public void isDead(){
        if (isDead == 1){
            getWorld().removeObject(this);         
        }      
    }
}
L2plex L2plex

2018/1/20

#
It's line 33
CxVercility CxVercility

2018/1/20

#
1
2
3
4
5
6
7
8
9
public void act()
   {
      move();      
      collisionCheck();
      setRotation(-90);
      hitEnemy();
        
      isDead();
   }
Well, if you constantly set the rotation to be -90°..
L2plex L2plex

2018/1/20

#
Hippo your code works perfectly fine!
L2plex L2plex

2018/1/20

#
I thougt I had it in the constructor, but I put it in the act(). Sorry... But thank you very much, Hippo und CxVercility!
danpost danpost

2018/1/20

#
CxVercility wrote...
Try this.getRotation() instead of just getRotation()
Makes no difference. The usage of 'this' in a non-static method is implicitly understood, if omitted.
There are more replies on the next page.
1
2