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

2015/1/11

A few questions for my game,

1
2
Socrates Socrates

2015/1/11

#
public void act()
    {
        getDirection(); //direction
        move(); //movement
        xSensor(); //if passes x coord spawn
    }
this is the act method for my player class sorry thougth you wanted the world class
danpost danpost

2015/1/11

#
I thought you said you were calling spawnTitles from the act method of the player 1 class? where is it?
Socrates Socrates

2015/1/11

#
yea this is the class it's spawning from, when the player passes ie. x coord of 250 it spawns another actor class (controls) onto the screen at 300, 250. This is happening in (xSensor()) I just changed the names
danpost danpost

2015/1/11

#
Fine. Now show the code for the three methods called from this act method.
Socrates Socrates

2015/1/11

#
import greenfoot.*;

public class Player extends Actor
{
    final int jSpeed = 30; // the initial 'jump' speed
    int ySpeed = 0, xSpeed = 0; // the initial vertical and horizontal speeds
    boolean aboutFace; // the direction (left or right) the actor is facing
    boolean onGround; // the state of the actor being set on an object or not

    /** 
     * Checks for changes in direction and moves the main actor.
     */
    public void act()
    {
        getDirection();
        move();
        xSensor();
    }
    
    public void xSensor()
    {
        if (getX() > 400)
        {
            getWorld().addObject(new controls(), 500, 500);
        }
    }
    
    /**
     * Moves the actor with appropriate image.  Checks for obstacles and adjusts
     * the position of the actor accordingly.
     */
    private void move()
    {
        ySpeed++; // adds gravity

        setLocation(getX()+xSpeed/10, getY()+ySpeed/2);
        // check for change in horizontal direction
        if((xSpeed>0 && aboutFace) || (xSpeed<0 && !aboutFace)) 
        {
            getImage().mirrorHorizontally();
            aboutFace = !aboutFace;
        }
        
        // check for obstacles
        onGround=false; // initialize value
        // check below the actor
        while(getOneObjectAtOffset(0, getImage().getHeight()/2+1, null)!=null)
        {
            setLocation(getX(), getY()-1); 
            onGround=true; 
            ySpeed=0;
        }
        // check above the actor
        while(getOneObjectAtOffset(0, -getImage().getHeight()/2-1, null)!=null) 
        {
            setLocation(getX(), getY()+1);
            ySpeed = 0;
        }
        // check to right of actor
        while(getOneObjectAtOffset(getImage().getWidth()/2+1, 0, null)!=null)
        {
            setLocation(getX()-1, getY());
            xSpeed = 0;
        }
        // check to left of actor
        while(getOneObjectAtOffset(-getImage().getWidth()/2-1, 0, null)!=null)
        {
            setLocation(getX()+1, getY());
            xSpeed = 0;
        }
    }

    /**
     * Determines any changes in horizontal and vertical speeds for the actor.
     */
    private void getDirection()
    {
//         if (!onGround) return; // if not mid-air changes allowed
        // sets requested direction of move, or continues in current direction
        if (Greenfoot.isKeyDown("left") && xSpeed>-50){
            move(-6);
        }// check left
        if (Greenfoot.isKeyDown("right") && xSpeed<50){
            move(6);
        } // check right
        if (Greenfoot.isKeyDown("up") && onGround) // check jump
        {
            ySpeed -= jSpeed; // add jump speed
        }
    }
}
Socrates Socrates

2015/1/11

#
by the way I really apreciate the help danpost, i've been really stumped
danpost danpost

2015/1/12

#
This looks like a hodge-podge. Different things from different scenarios and snippets from online help. It is no wonder you are having difficulties. (1) the 'getDirection' method is not a getter method that returns a direction; a better name for what is does might be 'actOnKeys'; (2) nowhere is the value of the horizontal speed field, 'xSpeed' being changed -- it start at a value of zero and all assignments set it to zero (you should either use the field or remove it); (3) you are still not using the 'false' parameter when adding the new controls object into the world;
Socrates Socrates

2015/1/12

#
yea thats exactly what it is lol and that makes sense now that I think about it. I didnt add false because I thought it was for making objects not scroll ( which is something else I was trying to make not scroll, the controls(actor) I want to scroll). I tried it though and I got an error, the line I have is : getWorld().addObject(new controls(), 500, 500, false); and the error is : method addObject in class greenfoot.World cannot be applied to given types. Required: greenfoot.Actor,int,int; found:controls,int,int,boolean; reason: actual and formal argument lists differ in length.
danpost danpost

2015/1/12

#
Ah, yes. The 'addObject' method with the boolean parameter is not in the World class, it is in the SWorld class:
((SWorld)getWorld()).addObject(/* actor, xLocation, yLocation, */, false);
should add a non-scrolling object from an Actor subclass. I believe that was stated in the SWorld documentation.
Socrates Socrates

2015/1/12

#
Alright thanks, it works now but I still have a problem with my actor disapearing (main class) when I add an overlay
You need to login to post a reply.
1
2