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

2019/3/1

Error trying to make colour detection

Underdemon Underdemon

2019/3/1

#
Trying to create colour detection in order to make accurate collision detection with the floor. My character code is here:
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Character here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Character extends Actor
{
    private int Speed = 0;
    private int acceleration = 1;
    private boolean jumping;
    private int jumpStrength = 11;
    private int speed = 2;
    /**
     * Act - do whatever the Character wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
 
    public void act()
    {
        Movement();
        checkFall();
    }   
 
    public void Movement()
    {
        if (Greenfoot.isKeyDown("d"))
        {
            move(3);
        }
        if (Greenfoot.isKeyDown("a"))
        {
            move(-3);
        }  
        if (Greenfoot.isKeyDown("w") && jumping == false)
        {
            Jump();
        }
    }
 
    public void Jump()
    {
        Speed = Speed - jumpStrength;
        jumping = true;
        Fall();
    }
 
    public void Fall()
    {
        setLocation(getX(), getY() + Speed);
        if (Speed <= 9)
        {
            Speed = Speed + acceleration;
        }
        jumping = true;
    }
 
    public void checkFall()
    {
        Level1 level1 = getWorldOfType(Level1.class);
        GreenfootImage img = level1.getBackground();
        java.awt.Color cLevel = img.getColorAt(getX(), getY());
        if(img.getColorAt(getX(), getY() + 24).cLevel.getRGB().equals("FF00FF00"))
        {
            Speed = 0;
            jumping = false;
        }
        else
        {
            Fall();
        }
    }
}
I'm getting an error here "if(img.getColorAt(getX(), getY() + 24).cLevel.getRGB().equals("FF00FF00"))", with the error saying cannot find variable cLevel P.S. I'm working in version 3.0.4 P.P.S I'm very new here so just tell me if anything is wrong please
danpost danpost

2019/3/1

#
You can never have a method local variable name (where the variable is declared within the method) both preceded and followed by a dot. In fact, a variable that references an object declared within a method can only be followed by a dot later on in that method. What you have on line 65 is a color dotted to another color, which makes no sense. You can use getRGB on either one individually. So, either remove "img.getColorAt(getX(), getY() + 24)." or remove "cLevel." from that line (depending on which color you want to get the RGB value from).
Underdemon Underdemon

2019/3/1

#
I removed cLevel so checkFall is now this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public void checkFall()
    {
        Level1 level1 = getWorldOfType(Level1.class);
        GreenfootImage img = level1.getBackground();
        java.awt.Color cLevel1 = img.getColorAt(getX(), getY());
        if(img.getColorAt(getX(), getY() + 24).getRGB()==("FF00FF00"))
        {
            Speed = 0;
            jumping = false;
        }
        else
        {
            Fall();
        }
    }
but this "==("FF00FF00")" throws up the error "incomparable types: int and java.lang.String", which i don't understand how i would fix it and if i use .equals instead, it says int cannot be dereferenced. (Line 6)
danpost danpost

2019/3/1

#
Underdemon wrote...
I removed cLevel so checkFall is now this: << Code Omitted >> but this "==("FF00FF00")" throws up the error "incomparable types: int and java.lang.String", which i don't understand how i would fix it and if i use .equals instead, it says int cannot be dereferenced. (Line 6)
You should probably remove the second import line and just compare the color, using equals, to Color.GREEN:
1
if (Color.GREEN.equals(img.getColorAt(getX(), getY() + 24)))
You can also remove line 5 in your last code post.
You need to login to post a reply.