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

2015/4/5

getImage() int returns

TURKEYNOODLE TURKEYNOODLE

2015/4/5

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public int getImInt()
    {
        if(getImage().equals(image1))
        {
            return 1;
        }
        else if(getImage().equals(image2))
        {
            return 1;
        }
        else if(getImage().equals(image3))
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
TURKEYNOODLE TURKEYNOODLE

2015/4/5

#
not sure why but it always returns 0, i cant get it to return 1 under any circumstance, i do have the image1, image2, image3 all defined in the constructor, any help?
Super_Hippo Super_Hippo

2015/4/5

#
'equals' is used for strings. I think you can simply use '=='. There is also a 'toString' method in the GreenfootImage class if you want to do it with the 'equal' method.
danpost danpost

2015/4/5

#
Super_Hippo wrote...
'equals' is used for strings. I think you can simply use '=='. There is also a 'toString' method in the GreenfootImage class if you want to do it with the 'equal' method.
'equals' is not just used for String objects. In fact, the 'equals' method is defined in the Object class, which means it is inherited EVERYWHERE. However, it needs to be overridden to change the way it compares objects -- and just to name a few places this is done: java.awt.Color, java.awt.Font, java.awt.Dimension, java.awt.Point, java.lang.String and java.util.Collection. If I am not mistaken, without overriding the method, using 'equals' would be the same as using '==', in that the objects compared must be the same object, not just similar objects. Overriding it usually will allow the comparison to be done at a deeper level, in most cases by comparing the contents of the objects (with String objects, it compares the character arrays for equality; with Color objects, it compares the color part values for equality). They do not have to be the same object, but similar, in the way defined by the method. The GrenfootImage class inherits the 'equals' method from the Object class and does not override it. Therefore, it will not help to use 'equals' over '==' for comparison (if I am correct). Make sure that you are using 'setImage' to set one of the images defined (image1, image2 or image3) to the actor. If you are still having problems with the method returning a value of '1', provide the entire class code for further investigation into the issue.
yedefei yedefei

2015/4/5

#
danpost wrote...
Make sure that you are using 'setImage' to set one of the images defined (image1, image2 or image3) to the actor.
That's the solution.
danpost danpost

2015/4/5

#
yedefei wrote...
< Quote Omitted > That's the solution.
Probably ... but not necessarily.
TURKEYNOODLE TURKEYNOODLE

2015/4/6

#
so, im not sure i understand how i coded this but, here is all of it
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class lilB here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class lilB extends Actor
{
        public int welk;
        public int timer;
        private double firePower = 3;
    private static final int reloadTime = 25;
    private int reload;
    private static final int reloaTime = 100;
    private int reloa;
    public int zz;
    public GreenfootImage image1 = new GreenfootImage("back_leftfoot.png");
    public GreenfootImage image2 = new GreenfootImage("back_rightfoot.png");
    public GreenfootImage image3 = new GreenfootImage("back_stop.png");
    public GreenfootImage image4 = new GreenfootImage("front_leftfoot.png");
    public GreenfootImage image5 = new GreenfootImage("front_rightfoot.png");
    public GreenfootImage image6 = new GreenfootImage("front_stop.png");
    public GreenfootImage image7 = new GreenfootImage("left_leftfoot.png");
    public GreenfootImage image8 = new GreenfootImage("left_rightfoot.png");
    public GreenfootImage image9 = new GreenfootImage("left_stop.png");
    public GreenfootImage image10 = new GreenfootImage("right_leftfoot.png");
    public GreenfootImage image11 = new GreenfootImage("right_righfoot.png");
    public GreenfootImage image12 = new GreenfootImage("right_stop.png");
    public lilB()
    {
        welk = 1;
        reload = 0;
        reloa = 0;
        zz=0;
    }
    public void act()
    {
        timer++;
        reload--;
        reloa--;
        if(Greenfoot.isKeyDown("w"))
                moveUp();
        if(Greenfoot.isKeyDown("s"))
                moveDown();
        if(Greenfoot.isKeyDown("d"))
                moveRight();
        if(Greenfoot.isKeyDown("a"))
                moveLeft();
        if(Greenfoot.isKeyDown("space") && reload < 1)
            attack();
        if(Greenfoot.isKeyDown("shift") && reloa < 1)
            attack2();
    }   
    public void moveUp()
    {
        int x = getX();
        int y = getY();
        if(timer%4==0)
        if(welk == 1)
        {      
            setImage("back_leftfoot.png");
                welk++;
            }
        else if(welk == 2)
                {
                setImage("back_stop.png");
                welk++;
                }
                else if(welk == 3)
                {
                setImage("back_rightfoot.png");
                welk++;
                }
        else
        {
            setImage("back_stop.png");
                welk = 1;
        }
         
        setLocation( x, y - 2 );
    }
    public void moveRight()
    {
        int x = getX();
        int y = getY();
        if(timer%4==0)
        if(welk == 1)
        {      
            setImage("right_leftfoot.png");
                welk++;
            }
        else if(welk == 2)
                {
                setImage("right_stop.png");
                welk++;
                }
                else if(welk == 3)
                {
                setImage("right_righfoot.png");
                welk++;
                }
        else
        {
            setImage("right_stop.png");
                welk = 1;
        }
 
        setLocation( x+2, y);
    }
    public void moveDown()
    {
        int x = getX();
        int y = getY();
        if(timer%4==0)
        if(welk == 1)
        {      
            setImage("front_leftfoot.png");
                welk++;
            }
        else if(welk == 2)
                {
                setImage("front_stop.png");
                welk++;
                }
                else if(welk == 3)
                {
                setImage("front_rightfoot.png");
                welk++;
                }
        else
        {
            setImage("front_stop.png");
                welk = 1;
        }
 
        setLocation( x, y + 2 );
    }
    public void attack()
    {
        if(getImage()==(image1) || getImage()==(image2) || getImage()==(image3))
            zz = 0;
        if(getImage()==(image4) || getImage()==(image5) || getImage()==(image6))
            zz = 90;
        if(getImage()==(image7) || getImage()==(image8) || getImage()==(image9))
            zz = 180;
        if(getImage()==(image10) || getImage()==(image11) || getImage()==(image12))
            zz = 270;
        reload = reloadTime;
        Ball b = new Ball();
         
        int fireDir =zz;
        b.addForce(new Vector(zz,firePower));
        getWorld().addObject(b, getX(),getY());
    }
    public void attack2()
    {
        if(getImage()==(image1) || getImage()==(image2) || getImage()==(image3))
            zz = 1;
        if(getImage()==(image4) || getImage()==(image5) || getImage()==(image6))
            zz = 2;
        if(getImage()==(image7) || getImage()==(image8) || getImage()==(image9))
            zz = 3;
        if(getImage()==(image10) || getImage()==(image11) || getImage()==(image12))
            zz = 4;
        reloa = reloaTime;
        WAV b = new WAV();
        WAV c = new WAV();
        WAV d = new WAV();
         
        b.addForce(new Vector(0,firePower));
        c.addForce(new Vector(35,firePower));
        d.addForce(new Vector(325,firePower));
        getWorld().addObject(b, getX(),getY());
        getWorld().addObject(c, getX(),getY());
        getWorld().addObject(d, getX(),getY());
    }
    public void moveLeft()
    {
        int x = getX();
        int y = getY();
        if(timer%4==0)
        if(welk == 1)
        {      
            setImage("left_leftfoot.png");
                welk++;
            }
        else if(welk == 2)
                {
                setImage("left_stop.png");
                welk++;
                }
                else if(welk == 3)
                {
                setImage("left_rightfoot.png");
                welk++;
                }
        else
        {
            setImage("left_stop.png");
                welk = 1;
        }
 
        setLocation( x-2, y);
    }
    public int ImInt()
    {
        if(getImage()==(image1))
        {
            return 1;
        }
        else if(getImage()==(image2))
        {
            return 1;
        }
        else if(getImage()==(image3))
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
 
}
moveup, movedown, moveleft, and moveright are used to make the character seem to move as you press the keys, the fire keys have two different shots, the problem i am currently having is trying to use an int where a rotation would add a force under vector class, i can only fire to the right, both attack and attack2 run 2 different attempts at this
danpost danpost

2015/4/6

#
First thing you need to do is to change all the 'setImage' String values to their equivalent field names. For example, change line 63 to:
1
setImage(image1);
Then, when you check to see if 'getImage() == image1', it should return true (when it actually is). You may also want to use a 'setImage' line in the constructor of the class ( 'public lilB()' ) so that it will start with a referenced image.
winnerpig winnerpig

2015/4/6

#
as up floor said ,“==” is comparing the reference of objects but not the content itself
You need to login to post a reply.