Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: [GM6] Easiest way to change block sprites?  (Read 1165 times)

0 Members and 1 Guest are viewing this topic.
[GM6] Easiest way to change block sprites?
« on: October 31, 2006, 12:31:57 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Female
  • Posts: 2374
I have a code for my game that lets me place the blocks I need in my room, and it will automaticly change sprite to best match based on objects around it. So, I would use something like...

Code: GML
  1. if !instance_place(x+32,y,obj_wall) and !instance_place(x-32,y,obj_wall) image_index = 1

But, I am not sure this is the best way. It is also a bit messy, and hard to keep track of. Does anyone know of a better way, or is this fine and dandy?
« Last Edit: March 01, 2007, 01:18:04 am by 4Sword »
Logged

Goodnight

Once and future Captain
Re: [GM6] Easiest way to change block sprites?
« Reply #1 on: October 31, 2006, 11:34:52 pm »
  • With a Capital G
  • *
  • Reputation: +1/-0
  • Offline Offline
  • Gender: Male
  • Posts: 706
Here's the super-fun binary method.

Arrange your subimages in order (from 0 to 15, I presume) so that when the number is expressed in binary, each bit represents whether or not there's another block next to this one in a certain direction.

0 (0000) = no blocks surrounding this
1 (0001) = a block above this
2 (0010) = a block below this
3 (0011) = blocks above and below this
4 (0100) = a block to the left of this
5 (0101) = blocks above and to the left of this
etc. and so on.

Now when the block is created, you can use those functions to check if there are other blocks around this one, and bit-shift each value.

I'd recommend using position_meeting() or place_meeting() instead of instance_position() or instance_place(). The "meeting" functions return only 0 (no) or 1 (yes), whereas the "instance" functions return the id of the instance it finds (which isn't necessary for this). Also, you might have to wait one step after the Create event (using an alarm) because when an instance is created, all the other ones around it might not be created yet.

Code: [Select]
image_index = place_meeting(x,y-1,objWall) + (place_meeting(x,y+1,objWall)<<1) + (place_meeting(x-1,y,objWall)<<2) + (place_meeting(x+1,y,objWall)<<3)
That code makes the image_index equal 1 if there's a block above, then adds 2 if there's one below, adds 4 if there's one to the left, and adds 8 if there's one to the right.

example here


[edit]
I went back and tried this in the Create event, without an alarm. Seems to work fine.
You will notice the block of 4 in the upper-right has a dot in the center. That's a result of how the sprites were drawn (to include a small dot for corners), not a bug with the code.
« Last Edit: November 01, 2006, 03:43:29 am by Goodnight »
Logged
Re: [GM6] Easiest way to change block sprites?
« Reply #2 on: November 01, 2006, 04:54:27 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Female
  • Posts: 2374
You rule, Goodnight! Thanks!  ;D
Logged
Re: [GM6] Easiest way to change block sprites?
« Reply #3 on: November 04, 2006, 08:54:29 pm »
  • Official Forum Hippie/Writer
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 200
Darn, goodnight beat me to it, lol.  I have a good example that uses a huge if structure instead of crazy math, but I have mine set up with a weird way of animating it.  I'll put up my example post-haste to show you what I mean...

[Edit] Nevermind, I can't find a good host...>_>  Besides, next to goodnight's, mine would seem superfluous...
« Last Edit: November 04, 2006, 08:56:20 pm by Bearer »
Logged

Goodnight

Once and future Captain
Re: [GM6] Easiest way to change block sprites?
« Reply #4 on: November 05, 2006, 04:08:12 am »
  • With a Capital G
  • *
  • Reputation: +1/-0
  • Offline Offline
  • Gender: Male
  • Posts: 706
Feel free to post it if you can.. somebody might learn something new from it. Lots of methods = lots of knowledge.

I did something like this in my game's demo, where the bush sprites all take formation automatically. Before coming up with the binary method, I used the four instance_position() checks to make a string; for example, if there are bushes above and to both sides, it would be "LRU". Then do this to make it so: execute_string("sprite_index=sprBush"+bushstring)
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.062 seconds with 49 queries.

anything