Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (492)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Parameters of method not working but work when directly coded  (Read 380 times)
0 Members and 1 Guest are viewing this topic.
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Posted 2013-05-17 22:43:59 »

Hey guys I have a rather odd (or it may just be my stupidity) question. I have a little animation method which is shown below:
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  
void setSprites(TextureRegion mainMob, TextureRegion upMob_STILL,
         TextureRegion upMob_LEFT, TextureRegion upMob_RIGHT,
         TextureRegion downMob_STILL, TextureRegion downMob_LEFT,
         TextureRegion downMob_RIGHT, TextureRegion rightMob_STILL,
         TextureRegion rightMob_LEFT, TextureRegion rightMob_RIGHT,
         TextureRegion leftMob_STILL, TextureRegion leftMob_LEFT,
         TextureRegion leftMob_RIGHT) {
      if (timer < 4)
         timer += .1f;
      else
         timer = 0;

      // walking right animation
     if (direction == 3) {
         if (timer < 1) {
            mainMob = rightMob_STILL;
         } else if (timer > 1 && timer < 2) {
            mainMob = rightMob_LEFT;
         } else if (timer > 2 && timer < 3) {
            mainMob = rightMob_STILL;
         } else if (timer > 3) {
            mainMob = rightMob_RIGHT;
         }
      }

      // walking left animation
     if (direction == 2) {
         if (timer < 1) {
            mainMob = leftMob_STILL;
         } else if (timer > 1 && timer < 2) {
            mainMob = leftMob_LEFT;
         } else if (timer > 2 && timer < 3) {
            mainMob = leftMob_STILL;
         } else if (timer > 3) {
            mainMob = leftMob_RIGHT;
         }
      }

      // walking up animation
     if (direction == 0) {
         if (timer < 1) {
            mainMob = upMob_STILL;
         } else if (timer > 1 && timer < 2) {
            mainMob = upMob_LEFT;
         } else if (timer > 2 && timer < 3) {
            mainMob = upMob_STILL;
         } else if (timer > 3) {
            mainMob = upMob_RIGHT;
         }
      }

      // walking down animation
     if (direction == 1) {
         if (timer < 1) {
            mainMob = downMob_STILL;
         } else if (timer > 1 && timer < 2) {
            mainMob = downMob_LEFT;
         } else if (timer > 2 && timer < 3) {
            mainMob = downMob_STILL;
         } else if (timer > 3) {
            mainMob = downMob_RIGHT;
         }
      }
   }

As you can see I have quite a lot of parameters, but that should all be working. I call the method appropriately, but the "mainMob" texture region is never changed. If I avoid using these parameters by doing this
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  
   void setSprites() {
      if (timer < 4)
         timer += .1f;
      else
         timer = 0;

      // walking Assets.right animation
     if (direction == 3) {
         if (timer < 1) {
            Assets.mainCreeper = Assets.rightCreeper_STILL;
         } else if (timer > 1 && timer < 2) {
            Assets.mainCreeper = Assets.rightCreeper_LEFT;
         } else if (timer > 2 && timer < 3) {
            Assets.mainCreeper = Assets.rightCreeper_STILL;
         } else if (timer > 3) {
            Assets.mainCreeper = Assets.rightCreeper_RIGHT;
         }
      }

      // walking left animation
     if (direction == 2) {
         if (timer < 1) {
            Assets.mainCreeper = Assets.leftCreeper_STILL;
         } else if (timer > 1 && timer < 2) {
            Assets.mainCreeper = Assets.leftCreeper_LEFT;
         } else if (timer > 2 && timer < 3) {
            Assets.mainCreeper = Assets.leftCreeper_STILL;
         } else if (timer > 3) {
            Assets.mainCreeper = Assets.leftCreeper_RIGHT;
         }
      }

      // walking Assets.up animation
     if (direction == 0) {
         if (timer < 1) {
            Assets.mainCreeper = Assets.upCreeper_STILL;
         } else if (timer > 1 && timer < 2) {
            Assets.mainCreeper = Assets.upCreeper_LEFT;
         } else if (timer > 2 && timer < 3) {
            Assets.mainCreeper = Assets.upCreeper_STILL;
         } else if (timer > 3) {
            Assets.mainCreeper = Assets.upCreeper_RIGHT;
         }
      }

      // walking Assets.down animation
     if (direction == 1) {
         if (timer < 1) {
            Assets.mainCreeper = Assets.downCreeper_STILL;
         } else if (timer > 1 && timer < 2) {
            Assets.mainCreeper = Assets.downCreeper_LEFT;
         } else if (timer > 2 && timer < 3) {
            Assets.mainCreeper = Assets.downCreeper_STILL;
         } else if (timer > 3) {
            Assets.mainCreeper = Assets.downCreeper_RIGHT;
         }
      }
   }


Everything works great this way. The "Assets.mainCreeper" is changed to whatever the current animation state should be. The only reason I can't do it like that is that I have many different types of mobs so I would want to use parameters rather than making a class or method specific for each mob type. Is there some specific reason that using parameters makes this not work at all? From my code (in the parameter example) everything is the same as the parameterless example which would make me think it would work. Is there something with using parameters that stops this from working properly? I know this may be a long question but I appreciate the help!

P.s. after the current image is set to the "mainMob" texture region or the "Assets.mainCreeper" i draw that texture region seems obvious but i thought i would include it.

Offline notk0

Senior Newbie





« Reply #1 - Posted 2013-05-17 22:50:33 »

Ok , It seems you are confusing lots of stuff.
JAVA is pass by value, and when you pass an object, you pass it's value (Which is it's "pointer" ) lets say that.
When you pass anything to a function, it just copies its value to a variable on the stack for that function.
When you change the value, you are changing it inside the stack on that function.
This is an usual beginner mistake.

try this.
int i = 6;
public void change(int i) { i =56; }  change(i) print(i)  and see what happens.
what happens is you set aside a variable for that function. get it?

it is as if you were doing.
Sprite s1; setSprite(Sprite s) will do s = s1; then s = newSprite; it doesn't affect your s1 that you passed.
How you fix it? you change the state of the sprite ( sprite.setStuff) , or you encapsulate the sprite in some object and change the state of that object, or you return a list or something of new textures/stuff
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #2 - Posted 2013-05-17 22:54:41 »

I was wondering if it was that, and it turns out it is. Would a getter /  setter solve this problem? Since calling a setter would change the original value rather than the copy, if my logic is right here...

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline HeroesGraveDev

JGO Kernel


Medals: 246
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #3 - Posted 2013-05-17 23:00:18 »

In basic terms:

You have a lot of objects stored in RAM.
There are 'pointers' which store the position of the object in RAM.

When you call a function, you pass a copy of the pointer to the function, which then finds the object which it points at.
Any changes to the object affect the object, which will change what's being referenced by all pointers, but:
If you reassign the variable, it changes the local value of the pointer, not the value of the object, and not the value of the pointer in other locations.

Here is a workaround to help explain my point:

1  
2  
3  
4  
5  
6  
7  
8  
9  
class Pointer <T extends Object>
{
   public T value;

   public Pointer(T value)
   {
      this.value = value;
   }
}

(Generics aren't neccessary, you can change it to just a certain class if you want)

Then you can change the value and it will change in all locations of the class.

DISCLAIMER: I do not recommend using that workaround. It was just to help explain the problem. Please just redesign your code.

Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #4 - Posted 2013-05-17 23:03:10 »

I think I understand what I am doing wrong here but what is a good way to redesign my code?

Offline HeroesGraveDev

JGO Kernel


Medals: 246
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #5 - Posted 2013-05-17 23:08:49 »

Remove mainMob from the method parameters as it is useless, change
setSprites()
to return mainMob, and when calling the function, do this:

1  
mainMob = setSprite(/*...all the sprites here etc.*/)


Also, this code has too much of a brute-force approach just for simple animations. I recommend redesigning for future projects.

Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #6 - Posted 2013-05-17 23:21:44 »

Everything is looking good now! Thank you. I too thought this looked a little excessive but I had no other ideas on how to make it simpler Sad

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (33 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (40 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (25 views)
2014-08-16 06:20:21

Tekkerue (37 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!