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

2019/8/23

constrcutor as an image (or whatever)

joejoe joejoe

2019/8/23

#
i have this code public player() { GreenfootImage myImage = getImage(); int myNewHeight = (int)myImage.getHeight()*3/10; int myNewWidth = (int)myImage.getWidth()*3/10; myImage.scale(myNewWidth, myNewHeight); } but i want the "player()" (or whatever it's called) to be applicable on an image i tried public GreenfootImage playerup() { GreenfootImage myImage = getImage(); int myNewHeight = (int)myImage.getHeight()*3/10; int myNewWidth = (int)myImage.getWidth()*3/10; myImage.scale(myNewWidth, myNewHeight); } (playerup is the image) but i already have a GreenfootImage playerup = new GreenfootImage("playerupside.png"); at the top of my code but it keeps saying that it is missing return statement
danpost danpost

2019/8/23

#
It says that because you have the return type set to a GreenfootImage object (first line after "I tried"). Either make the return type void or add a return statement at the end of the playerup method. Maybe:
return myImage;
joejoe joejoe

2019/8/23

#
it still has a problem (ill write the whole code)
/**
 * Write a description of class player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class player extends Actor
{
    public int mouseX, mouseY;
    public static int playerX, playerY;
    public int playerRotation;
    private boolean spaceDown;
    GreenfootImage playerup = new GreenfootImage("playerupside.png");
    
    

    public player()
    {
        GreenfootImage myImage = getImage();
        int myNewHeight = (int)myImage.getHeight()*3/15;
        int myNewWidth = (int)myImage.getWidth()*3/15;
        myImage.scale(myNewWidth, myNewHeight);

    }
the problem is that when i place the actor on the world, when placed down, it will automatically shrink to its size (indicated at the code) what i want to happen is that when i press up or "w", the picture or "playerup" picture will also shrink to its size but instead, it goes to its normal png size
joejoe joejoe

2019/8/23

#
i just used that kind of coding instead of using setRotation because if i used setRotation, the picture will move upward but a little bit (this is a problem because i am doing a game of a maze). i wanted the actor to just turn 90 degrees or whatever so that the actor will just perfectly turn or rotate 90 degrees or whatever (i hope you get what i mean) (sorry for such questions bc i just got introduced to greenfoot)
danpost danpost

2019/8/23

#
You are working with, and presumably going back and forth between, two images. However, you only have a field for one of them. You need a field for the other one (initial one) or you will lose that image when you go to the playeup image. You may want to use an array for the two images::
private GreenfootImage[] images = new GreenfootImage[2];

public player()
{
    images[0] = getImage();
    images[0].scale(images[0].getWidth()*3/5, images[0].getHeight()*3/5);
    images[1] = new GreenfootImage("playerupside.png");
    images[1].scale(images[1].getWidth()*3/5, images[1].getHeight()*3/5);
    setImage();
}
I used a call to a new method called setImage (with no parameters) to assist during game play:
private void setImage()
{
    setImage(images[getRotation()/180]);
}
joejoe joejoe

2019/8/24

#
i workd perfectly but the problem is that it only move horizontally, if i use setRotation, the image would also turn instead of the movement only
danpost danpost

2019/8/24

#
joejoe wrote...
i workd perfectly but the problem is that it only move horizontally, if i use setRotation, the image would also turn instead of the movement only
If I understand you correctly, then maybe you could try flipping the image by adding the following after my line 8 above:
images[1].mirrorVertically();
danpost danpost

2019/8/24

#
I am aware that there is a red-shift involved with that method (the mirrorVertically one) and it may not quite do what you want. As an alternative, try this instead:
public player()
{
    images[0] = getImage();
    images[0].scale(images[0].getWidth()*3/5, images[0].getHeight()*3/5);
    images[1] = new GreenfootImage(images[0]); // for sizing
    GreenfootImage line = new GreenfootImage(images[0].getWidth(), 1);
    for (int i=0; i<images[0].getHeight(); i++)
    {
        line.drawImage(images[0], -i, 0);
        images[1].drawImage(line, images[1].getHeight()-1-i, 0);
    }
    setImage();
}
joejoe joejoe

2019/8/24

#
its finally working, thanks a lot. i'll let you know if i need anything. thanks again for your time :)
You need to login to post a reply.