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

2013/4/1

How to access one class's rotation from another class

1
2
JetLennit JetLennit

2013/4/1

#
Yeah.. that was just the only way i knew without a for loop and a delay...
Griffon305 Griffon305

2013/4/1

#
After I put that code for the direction of the bullet, it still shoots to the right no matter which direction the turret is angled at. Am I missing something in the bullet class? This is what I have so far:
1
2
3
4
5
6
public void act() 
   
       setLocation(getX() + speed, getY()); 
       checkBoundaries(); 
       destroyEnemies(); 
   }
(I am ommitting the checkBoundaries and destroyEnemies methods). Do I need to set the rotation of the bullet to that of the turret? In my last discussion I thought danpost provided me with code that wouldn't make that necessary, but I could have easily misinterpreted. Thanks!
JetLennit JetLennit

2013/4/1

#
what about the firing code?
danpost danpost

2013/4/1

#
The code you supplied does not assist us in determining your issue. Show the code in the Turret class that creates and adds the bullet to the world. Also, it may help if you show the code you use to rotate the turret itself.
JetLennit JetLennit

2013/4/1

#
I get it..... try putting instead of your current move statement
1
move(speed);
danpost danpost

2013/4/1

#
JetLennit wrote...
I get it..... try putting instead of your current move statement
1
move(speed);
Good catch (I missed that).
JetLennit JetLennit

2013/4/1

#
I almost missed it too.... except that i had made the same mistake =)
Griffon305 Griffon305

2013/4/1

#
I have to go to work, but I will post it when I get back tonight
Griffon305 Griffon305

2013/4/2

#
Ok, so here is the code for the turret, danpost you actually helped me with the turret rotation previously:
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
public void act()
    {
         
        checkKeyPress();
         
         
    }   
    /**
     * This will check to see if a key has been pressed. If the left arrow is pressed, the turret
     * will rotate counterclockwise.  If the right arrow is pressed, the turret will turn clockwise.
     */
    // add instance field 
    public int rotation = 0
    // the method 
    public void checkKeyPress() 
    
        if (Greenfoot.isKeyDown("left") && rotation >-90
        
            turn(-5); 
            rotation -= 5
        
        if (Greenfoot.isKeyDown("right") && rotation < 90
        
            turn(5); 
            rotation += 5
        
         
        if(Greenfoot.isKeyDown("space"))
        
            bullet bullet = new bullet(); 
            bullet.setRotation(rotation); 
            getWorld().addObject(bullet, getX(), getY());
        }
    }
Any thoughts on what is not working? Thank you!
danpost danpost

2013/4/2

#
1
2
3
4
5
6
7
8
9
10
11
12
// add this instance field
private boolean spaceDown;
// replace lines 28 on with
    if (!spaceDown && Greenfoot.isKeyDown("space"))
    {
        spaceDown = true;
        bullet bullet = new bullet();
        bullet.setRotation(rotation);
        getWorld().addObject(bullet, getX(), getY());
    }
    if (spaceDown && !Geenfoot.isKeyDown("space")) spaceDown = false;
}
You need to login to post a reply.
1
2