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

2018/12/26

Rotating canon from the end

1
2
3
dev4ltar dev4ltar

2018/12/26

#
i have a tank canon "canon.png" and i want to rotate it from the edge not from middle and i still confused about this code :'( where to place it? on my canon?
public void act() 
    {
        // Add your action code here.
        canonMove();
        GreenfootImage canon = getImage();
        GreenfootImage image = new GreenfootImage(2*canon.getWidth(), canon.getHeight()); //no error on compile, but it's eror on this lineon run
        image.drawImage(canon, 210,360);
        setImage(image);
    }    
    private void canonMove(){
        MouseInfo mouse = Greenfoot.getMouseInfo();
        int rx = mouse.getX()-getX();
        int ry = mouse.getY()-getY();
        int angle = (int) (Math.atan2(ry,rx)*180.0/Math.PI);
        setRotation(angle);
    }
Thanks for helping :')
danpost danpost

2018/12/26

#
dev4ltar wrote...
i have a tank canon "canon.png" and i want to rotate it from the edge not from middle and i still confused about this code :'( where to place it? on my canon? << Code Omitted >>
From what I can tell, line 5 through 8 should only need to be once. Therefore, those lines would be better placed either in the class constructor or in an overriding addedToWorld method. The other thing I see is that you could make use of the turnTowards method, rather than using setRotation in the canonMove method.
dev4ltar dev4ltar

2018/12/26

#
thanks about the turnTowards here's my code now, i removed the line 5-8 because i still confused :(
//on canon
public class Canon extends Actor
{
    public void act() 
    {
        canonMove();

    }    
    private void canonMove(){
        MouseInfo mouse = Greenfoot.getMouseInfo();
        turnTowards(mouse.getX(),mouse.getY());
    }
}
about line 5-8 can you give an example?, sorry for asking too much and thanks for helping is it right? my canon disappeared after running the code on 1st post (not this reply)
// on World>MyWorld
private void prepare()
    {
        Hero hero = new Hero();
        Canon canon = new Canon();
        addObject(canon,210,360);
        addObject(hero,100,370);
        addObject(scoreCounter, 50,20);
        addObject(liveCounter, 150,20);
        scoreCounter.setValue(score);
        liveCounter.setValue(live);
    }
danpost danpost

2018/12/26

#
dev4ltar wrote...
about line 5-8 can you give an example? my canon disappeared after running the code on 1st post (not this reply)
I am puzzled. Your drawing offsets on line 7 are huge. How big is your initial canon image? also, how big is the actual canon within the initial image?
dev4ltar dev4ltar

2018/12/27

#
125x13 on paint here's the image, sorry for long respond, thanks for keep helping me
danpost danpost

2018/12/27

#
dev4ltar wrote...
125x13 on paint here's the image, << Image Omitted >>
Then the double sized image is 250x13. You should not be drawing (line 7) past (125, 0).
dev4ltar dev4ltar

2018/12/27

#
like this?
public void act() 
    {
        // Add your action code here.
        canonMove();
        GreenfootImage square = getImage();
        GreenfootImage image = new GreenfootImage(2*square.getWidth(), square.getHeight()); 
        image.drawImage(square, 125,0);
        setImage(image);
    }    
no compile error, but after i run it say error on line 6
dev4ltar dev4ltar

2018/12/27

#
the canon fly then disappear first then that error^ i just want to rotate the canon from the left end, like a canon on tanks thanks for helping
danpost danpost

2018/12/27

#
dev4ltar wrote...
the canon fly then disappear first then that error^ i just want to rotate the canon from the left end, like a canon on tanks thanks for helping
Ehh, why is that in the act method (just noticed, again). The image should be fixed during canon construction. With it in the act method, the image will double in size each act step which will cause the image of the canon to fly off the left edge of the screen in short order. Try this:
import greenfoot.*;

public class Canon extends Actor
{
    public Canon()
    {
        GreenfootImage img = getImage();
        GreenfootImage image = new GreenfootImage(2*img.getWidth(), img.getHeight());
        image.drawImage(img, 125, 0);
        setImage(image);
    }
    
    public void act()
    {
        canonMove();
    }

    // etc.
}
dev4ltar dev4ltar

2018/12/27

#
Thank you very much it work now :'D is the method drawing a blank image on the left of the canon?
dev4ltar dev4ltar

2018/12/27

#
and make the left end of the canon become the middle part of the image?
danpost danpost

2018/12/27

#
dev4ltar wrote...
Thank you very much it work now :'D is the method drawing a blank image on the left of the canon? and make the left end of the canon become the middle part of the image?
Your line 8 is creating a blank image and line 9 draws the canon on the right half of that image, making the middle of the image frame at the left end of the actual canon image.
dev4ltar dev4ltar

2018/12/27

#
and 1 more question, how to set max angle of rotation? like max angle is 60 degrees
private void canonMove(){
        MouseInfo mouse = Greenfoot.getMouseInfo();
        turnTowards(mouse.getX(),mouse.getY());
    }
is it like this?
private void maxAngle(){
int angle = canon.getRotation();
return angle > 0 && angle <60;
}
danpost danpost

2018/12/27

#
dev4ltar wrote...
and 1 more question, how to set max angle of rotation? like max angle is 60 degrees
And min of 0? How about:
private void canonMove()
{
    MouseInfo mouse = Greenfoot.getMouseInfo();
    int y = mouse.getY();
    if (y > getY()) setRotation(0); else turnTowards(mouse.getX(), y);
    if (getRotation() < 300 && getRotation() != 0) setRotation(300);
}
Remember rotation increases clockwise, so from the right horizontal, raising the canon goes in the negative angular direction. So, you want angles from 0 to -60 (which is 360 down to 300).
dev4ltar dev4ltar

2018/12/27

#
thank you very much, you help a lot, sorry if i ask too much, have a nice day :D is there a way to make it more smoother to place the canon to 0 degree and 300?
There are more replies on the next page.
1
2
3