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

2017/9/18

How do i let an actor spawn 90 degrees turned?

Recorsi Recorsi

2017/9/18

#
At the start i let an actor spawn(spaceship), but to avoid some errors i had to turn the image 270 degrees(or 90 degrees counter clockwise). It is now spawning facing the left. how do i let it spawn straight?
1
2
3
4
5
private void NewShip()
   {
     addObject(new Spaceship(), 320, 450); 
     spaceship.setRotation(90); 
   }
setRotation doesn't work thank you :)
danpost danpost

2017/9/18

#
Recorsi wrote...
setRotation doesn't work
It is not that it doesn't work -- it is that it cannot work because it needs an Actor object to work on. 'spaceship' appears to be undeclared (an attempt to use a variable that comes out of thin air). All variables must be declared before they are used. A line of code that declares a variable starts with the variable type, followed by the name you give the variable:
1
Spaceship spaceship;
'Spaceship' is the type of object that the variable 'spaceship' is to hold. Declaring it, however, does not assign any object to that variable. It still needs the object to be assigned to it. This can be done on the same line as follows:
1
Spaceship spaceship = new Spaceship();
Now that 'spaceship' references an object, its rotation can be set and it can be added into the world:
1
2
3
Spaceship spaceship = new Spaceship();
addObject(spaceship, 320, 450);
spaceship.setRotation(90);
Recorsi Recorsi

2017/9/18

#
danpost wrote...
Recorsi wrote...
setRotation doesn't work
It is not that it doesn't work -- it is that it cannot work because it needs an Actor object to work on. 'spaceship' appears to be undeclared (an attempt to use a variable that comes out of thin air). All variables must be declared before they are used. A line of code that declares a variable starts with the variable type, followed by the name you give the variable:
1
Spaceship spaceship;
'Spaceship' is the type of object that the variable 'spaceship' is to hold. Declaring it, however, does not assign any object to that variable. It still needs the object to be assigned to it. This can be done on the same line as follows:
1
Spaceship spaceship = new Spaceship();
Now that 'spaceship' references an object, its rotation can be set and it can be added into the world:
1
2
3
Spaceship spaceship = new Spaceship();
addObject(spaceship, 320, 450);
spaceship.setRotation(90);
I don't know why but it still doesn't turn
Super_Hippo Super_Hippo

2017/9/18

#
Is the NewShip method executed? So is the ship in the world really added from that code? Do you override the rotation? For example in the act method of the ship.
Recorsi Recorsi

2017/9/18

#
Yes the newship method is executed. i removed it temporarily and no ship spawned. Here is the world code:
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Spielfeld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Spielfeld extends World
{
    Spaceship spaceship = new Spaceship();
    Rock rock = new Rock();
    GifImage background = new GifImage("background.gif");
    GreenfootSound Ton2 = new GreenfootSound("Musik1.mp3");
    GreenfootSound Ton3 = new GreenfootSound("Musik2.mp3");
    GreenfootSound Ton4 = new GreenfootSound("Musik3.mp3");
    GreenfootSound Ton5 = new GreenfootSound("Musik4.mp3");
    /**
     * Constructor for objects of class Spielfeld.
     *
     */
    public Spielfeld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(640, 480, 1);
        NewShip();
        placeRocks();
    }
    public void placeRocks()
    {
       for (int i = 0; i < 10; i++) {
        int x = Greenfoot.getRandomNumber( 640);
        int y = Greenfoot.getRandomNumber( 370);
        addObject ( new Rock(), x , y); 
        rock.setRotation(Greenfoot.getRandomNumber(360));
        }
    }
     
    public void started() {
      switch(Greenfoot.getRandomNumber(4))
    {
        case 0:  Greenfoot.playSound("Musik1.mp3"); break;
                 
        case 1:  Greenfoot.playSound("Musik2.mp3"); break;
                 
        case 2:  Greenfoot.playSound("Musik3.mp3"); break;
         
        case 3:  Greenfoot.playSound("Musik4.mp3"); break;
                  
    }
    }
    public void stoped() {
      Ton2.pause();
    }
    private void NewShip()
    {
      addObject(new Spaceship(), 320, 450); 
      spaceship.setRotation(90); 
    }
    public void act()
    {
        setBackground( background.getCurrentImage() );
         
    }
     
}
and here the ship code:
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import greenfoot.*; 
 
/**
 * Write a description of class Spaceship here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Spaceship extends Actor
{
   private static final int NUM_FRAGMENTS = 20;
   GifImage myGif = new GifImage("spaceship.gif");
   private int dx;
   private int dy;
   Laser laser = new Laser();
   GreenfootSound Ton1 = new GreenfootSound("Laser.wav");
   private int horizontalSpeed = 5;
   private int verticalSpeed = 5;
   explosion explosion = new explosion();
   public Spaceship()
   {
      GreenfootImage image = getImage(); 
      image.scale(75, 75);
      setImage(image);
   }   
   public void act()
   {
      Bewegen();
      pruefeKontaktRand();
      shoot();
      GIF();
      TriggerEx();
   }
   public boolean TriggerEx()
    {
        if (getOneIntersectingObject(Rock.class) != null)
        {
            explode2();
            //Explosion noch nicht funktionabel
            //getWorld().addObject(explosion, getX()+1, getY()+1);
        }
        return false;
    }
   private void GIF()
   {
       setImage( myGif.getCurrentImage() );
       GreenfootImage image = getImage();
       image.scale(75, 75);
       setImage(image);
    }
   private void Bewegen()
   {
      if (Greenfoot.isKeyDown("a"))
       {
            turn(-4);
       }
      if (Greenfoot.isKeyDown("d"))
       {
            turn(4);
       }
      if (Greenfoot.isKeyDown("w"))
       {
            move(-5);
       }
       
      /*if (Greenfoot.isKeyDown ("w") && Greenfoot.isKeyDown("a"))
      {
          setRotation(310);
           
        }
      if (Greenfoot.isKeyDown ("w") && Greenfoot.isKeyDown("d"))
      {
          setRotation(50);
          
        }
      if (Greenfoot.isKeyDown ("s") && Greenfoot.isKeyDown("a"))
      {
          setRotation(225);
          
        }
      if (Greenfoot.isKeyDown ("s") && Greenfoot.isKeyDown("d"))
      {
          setRotation(140);
           
        
  */ }
  private void pruefeKontaktRand()
  {
    if (getX() >=626) {
        dx = -dx;
        
    if (getX() <=14) {
        dx = -dx;
        }
    if (getY() >=466) {
        dy = -dy;
        }
    if (getY() <=14) {
        dy = -dy;
        }
}
/*private void shoot()
{
    if(Greenfoot.isKeyDown("space"))
    {
       World Spielfeld = getWorld();
       Spielfeld.addObject(laser, 0, 0);
       laser.setLocation(getX(), getY());
       laser.setRotation(getRotation()-90);
       Ton1.play();
       Ton1.setVolume(75);
    }
}*/
private void shoot()
 {
  if("space".equals(Greenfoot.getKey())){
    Laser laser = new Laser();
    getWorld().addObject(laser, getX(), getY());
    laser.setRotation(getRotation()-180);
    laser.move(40);
    Ton1.play();
  }
 }
public void explode2()
 {
   placeDestroyed (getX(), getY(), NUM_FRAGMENTS);
   getWorld().removeObject(this);
    
 }
private void placeDestroyed(int x, int y, int numFragments)
{
   for (int i=0; i < numFragments; i++) {
       getWorld().addObject(new Destroyed(), x ,y );
    }
 }
}
sorry for this large message ^^
Super_Hippo Super_Hippo

2017/9/18

#
1
addObject(new Spaceship(), 320, 450);
The spaceship you add there is not the same as the 'spaceship' variable. Change the line to what danpost gave you:
1
addObject(spaceship, 320, 450);
Recorsi Recorsi

2017/9/18

#
ohhhhh, i didnt see that sry thanks for helping! :D
You need to login to post a reply.