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

2017/8/5

Can't program a class parameter?

DankBoi DankBoi

2017/8/5

#
I'm updating a method I wrote to include a parameter to pass into the method getOneIntersectingObject. It needs a parameter of type class, but I have no idea how to program a class parameter as part of a user-defined method. My attempts to do so resulted in failure. Can you help me?
Venbha Venbha

2017/8/5

#
You are making to detect to touch one object with another. Why not try
1
2
3
4
if(isTouching(Object.class))
{
       // apply what you want to do with it
}
If you haven't imported animal class. Or if you have,
1
2
3
4
if(canSee(Object.class))
{
       // apply what you want to do with it
}
try them. In other cases you want only getOneIntersectingObject, you can ctrl+space, type that in your code window and read about it beside it.
Venbha Venbha

2017/8/5

#
it says greenfoot.Actor Actor getOneIntersectingObject(Class<?>) Return an object that intersects this object. This takes the graphical extent of objects into consideration. Parameters    cls - Class of objects to look for (passing 'null' will find all objects). return - An object of the given class type that intersects with the current object.
Super_Hippo Super_Hippo

2017/8/5

#
DankBoi wrote...
I have no idea how to program a class parameter as part of a user-defined method.
You can use "Class" the same way as other types, like this:
1
2
3
4
public void methodName(Class cls)
{
    //do something
}
DankBoi DankBoi

2017/8/6

#
I was able to get that to work, but now I have a new problem. I got what I'm pretty sure were these exact same lines of code to work before I edited them (I changed them back), and not they don't work. My character can't move well, he slides around without stopping like he's on an ice rink or something, only stopping when he hits an object (hittableObject). I have no idea what's going on and it's almost identical to code posted by Danpost here: https://www.greenfoot.org/topics/1747 Here is the code I'm having trouble with: (it's the checkForArrowKeys method)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class PlayerCharacter here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class PlayerCharacter extends Actor
{
    int dx = 0;
    int dy = 0;
    /**
     * Act - do whatever the PlayerCharacter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        checkForArrowKeys(5, Tent.class);
        //checkForCollision(Tent.class);
    }   
     
    public void checkForArrowKeys(int adjust, Class hittableObject)
    {       
        if ( Greenfoot.isKeyDown("right") )
        {
            dx+=1;
        }
        if ( Greenfoot.isKeyDown("left") )
        {
            dx-=1;
        }
        if ( Greenfoot.isKeyDown("down") )
        {
            dy+=1;
        }
        if ( Greenfoot.isKeyDown("up") )
        {
           dy-=1;
        }
         
        for (int i = 0; i< adjust; i++)
        {
            setLocation(getX() + dx, getY());
            setLocation(getX(), getY() + dy);
            if (getOneIntersectingObject(hittableObject) != null)
            {
                setLocation(getX() - dx, getY());
                 
            }
            if (getOneIntersectingObject(hittableObject) != null)
            {
                setLocation(getX(), getY() - dy);
            }
        }       
         
    }
     
     
}
Super_Hippo Super_Hippo

2017/8/6

#
Try to move line 10 and 11 to the beginning of the checkForArrowKeys method.
DankBoi DankBoi

2017/8/7

#
Thanks! That solved the issue.
Venbha Venbha

2017/8/7

#
:)
danpost danpost

2017/9/2

#
Super_Hippo wrote...
Try to move line 10 and 11 to the beginning of the checkForArrowKeys method.
For others who might review this discussion thread. I am quite sure the intended lines were 11 and 12.
You need to login to post a reply.