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

2015/1/9

Help Needed! <identifier> expected

TheGoldenBucket TheGoldenBucket

2015/1/9

#
Hi, I needed some help with my new game... I am trying to change the image when the character moves up and down, and I am having trouble creating the variable.
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class PlayerChar here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class PlayerChar extends Actor
{
    /**
     * Act - do whatever the PlayerChar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    charImage++;
 
    public void act()
    {
        if (Greenfoot.isKeyDown("up"))
        {
            int x = getX();
            int y = getY();
            int ny = getY()-1;
            setLocation(x,ny) ;
            GreenfootImage charImage = new GreenfootImage("IMG_1191.PNG");
        }
        if (Greenfoot.isKeyDown("down"))
        {
            int x = getX();
            int y = getY();
            int ny = getY()+1;
            setLocation(x,ny) ;
            GreenfootImage charImage = new GreenfootImage("IMG_1190.PNG");
        }
        if (Greenfoot.isKeyDown("left"))
        {
            int x = getX();
            int y = getY();
            int ny = getX()-1;
            setLocation(ny,y) ;
        }
        if (Greenfoot.isKeyDown("right"))
        {
            int x = getX();
            int y = getY();
            int ny = getX()+1;
            setLocation(ny,y) ;
        }
        setImage(charImage);
    }   
}
GRIFFIN GRIFFIN

2015/1/9

#
The reason that this code isn't working unless I'm mistaken, is because the variable charImage is a local variable created every time the if statement runs. To fix it, you will have to create the variable charImage outside of the if statement, and not declare a new variable each time. Just change the variable, not make a new one.
danpost danpost

2015/1/9

#
First, remove line 15 (it is totally out of place in that (1) charImage is undeclared within the class (2) the line is an executable statement that cannot be executed outside of a code block (method or constructor) and (3) it appears to be an field of type int that is not used anywhere else in the class. Next, you are declaring a local GreenfootImage variable within two different 'if' blocks (see lines 25 and 33). The scope of these variables (how long it remains available for use) are limited to the 'if' code blocks they are in. So, one charImage field is different than the other; and both are lost immediately after being created (the 'if' code block ends immediately after each at lines 26 and 34). If the 'charImage' referred to in line 49 is to have any meaning, the variable must be declared at that level or at a shallower level (inside less brackets is allowed -- within more is not). Insert at line 19 the following line:
1
GreenfootImage charImage = null;
Then remove the FIRST 'GreenfootImage' from both lines 25 and 33. If they were left in, the variable now declared on line 19 would be shadowed by the more local variable with the same name (that, you do not want). Finally, we must deal with the possible that 'charImage' will still be null at line 49. Change line 49 to:
1
if (charImage != null) setImage(charImage);
TheGoldenBucket TheGoldenBucket

2015/1/9

#
Thanks guys. Got it working, and now I'm off to create walls. -TheGoldenBucket
You need to login to post a reply.