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

2021/7/4

giving X, Y, and Rotation & calling a method from an actor to myWorld

Gabe1098 Gabe1098

2021/7/4

#
is getX() getY() getRotation() like giving the x position, y position and direction? here is my code so far
if (Greenfoot.isKeyDown("space")) {
            createBullet(getX(), getY(), getRotation());
        }
danpost danpost

2021/7/4

#
Gabe1098 wrote...
is getX() getY() getRotation() like giving the x position, y position and direction?
I believe you understand correctly. But you have not provided anything involving:
calling a method from an actor to myWorld
Gabe1098 Gabe1098

2021/7/4

#
danpost wrote...
Gabe1098 wrote...
is getX() getY() getRotation() like giving the x position, y position and direction?
I believe you understand correctly. But you have not provided anything involving:
calling a method from an actor to myWorld
the method is in the myWorld and this is in the player class but it says cannot find method "createBullet"
danpost danpost

2021/7/4

#
Gabe1098 wrote...
the method is in the myWorld and this is in the player class but it says cannot find method "createBullet"
But, you are calling the method on the player, not on your world. Note that:
createBullet(a, b, c);
// is equivalent to
this.createBullet(a, b, c);
("this' being implicitly understood to be the player if coded in the class of the player) which will only find the method if it belongs to the player. All method calls have the format of { subject.verb },, where the "verb" is defined for the "subject", whether given explicitly, or not. If the "verb" createBullet is defined for your world, it -- the world -- must be explicitly given to call it from the class of the player. Also, it must be given as of a type the method is valid for -- as a MyWorld object:
((MyWorld)getWorld()).createBullet(a, b, c);
Gabe1098 Gabe1098

2021/7/4

#
danpost wrote...
Gabe1098 wrote...
the method is in the myWorld and this is in the player class but it says cannot find method "createBullet"
But, you are calling the method on the player, not on your world. Note that:
createBullet(a, b, c);
// is equivalent to
this.createBullet(a, b, c);
("this' being implicitly understood to be the player if coded in the class of the player) which will only find the method if it belongs to the player. All method calls have the format of { subject.verb },, where the "verb" is defined for the "subject", whether given explicitly, or not. If the "verb" createBullet is defined for your world, it -- the world -- must be explicitly given to call it from the class of the player. Also, it must be given as of a type the method is valid for -- as a MyWorld object:
((MyWorld)getWorld()).createBullet(a, b, c);
it says "cannot find symbol - class myWorld"
if (Greenfoot.isKeyDown("space")) {
            ((myWorld)getWorld()).createBullet(getX(), getY(), getRotation());
        }
danpost danpost

2021/7/4

#
Gabe1098 wrote...
it says "cannot find symbol - class myWorld
Show world class codes (full page).
Gabe1098 Gabe1098

2021/7/4

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        Player player = new Player();
        addObject(player,300,200);
        Bullet bullet = new Bullet();
    }
    public void createBullet() {
        
    }
}
danpost danpost

2021/7/5

#
You need to cast to MyWorld, not myWorld.
Gabe1098 Gabe1098

2021/7/5

#
danpost wrote...
You need to cast to MyWorld, not myWorld.
Thank You!!! but now I have a new error... it says "method createBullet in class MyWorld cannot be applied to given types: required no arguments: found: int, int, int reason: actual and formal arguments lists differ in length"
danpost danpost

2021/7/5

#
Gabe1098 wrote...
I have a new error... it says "method createBullet in class MyWorld cannot be applied to given types: required no arguments: found: int, int, int reason: actual and formal arguments lists differ in length"
Add parameters to the method (line 24) to suit your arguments (those values you are trying to pass to the method).
Gabe1098 Gabe1098

2021/7/9

#
danpost wrote...
Gabe1098 wrote...
I have a new error... it says "method createBullet in class MyWorld cannot be applied to given types: required no arguments: found: int, int, int reason: actual and formal arguments lists differ in length"
Add parameters to the method (line 24) to suit your arguments (those values you are trying to pass to the method).
Ooohhh lol I feel so dumb xD Thanks!
You need to login to post a reply.