Post new topic    
Liquid Metal Slime
Send private message
Problem: Bullet slices not originating from hero pixel (x,y) 
 PostFri Dec 19, 2014 5:19 pm
Send private message Reply with quote
I've kind of been thinking about adding projectiles in to my game. I decided to do a simple bullet which is supposed to originate from the hero's pixel x, and pixel y position before flying off in to the distance. It seemed to be working fine but I just realized that when I move away from the top left corner of the map it seems to shift the bullets origin...

Here's the current code:
Code:

variable(collection, sl)
    if(key is pressed(key:X)) then(
      collection := load slice collection(10)
      sl := lookup slice(sli:Bullet1, collection)
      put slice (sl, hero pixel X (me), hero pixel Y (me))
      set slice velocity x (sl, 4)
      set slice velocity y (sl, 0)
    )


Here's a video showing the issue graphically:
https://www.youtube.com/watch?v=rQWFwlfBUG0&list=PL5FafLrq9rotB0E1nGNIUkehH6wejO_Rr

EDIT: On a related note how do I make my code so that only one instance of a bullet slice can exist at a time? I want to make it so the player cannot shoot a second bullet until the first one either hits it's target, a wall, or disappears after a set amount of ticks.
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
⍠ C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
Liquid Metal King Slime
Send private message
 
 PostFri Dec 19, 2014 8:04 pm
Send private message Reply with quote
If you're using another script to control movement or hero display. You'll need to relate to the hero by using the variables you've assigned to the hero's pixels in that other script.


As far as making your code allow only one bullet at a time. That's so simple it's startling you'd ask. You set a tag or variable on when the bullet is there and off when it disappears. While having the variable/tag control whether the player can shot or not.
Metal King Slime
Send private message
 
 PostFri Dec 19, 2014 8:28 pm
Send private message Reply with quote
Your problem is you're mixing up your references. Hero Pixel X and Y refer to the Heroes position on the *map* in pixels. Slices are based on the position on *screen* and they follow the camera, so a value higher than the screen resolution in any axis (320x200) means the slice is off screen and effectively invisible.

What's happening, is that as the hero moves, the camera follows him. Let's say you're on tile 20,20. You say "What Pixel on the map is the hero on?" and the game answers "400,400". Then you draw your slice 400 pixels to the right, and 400 pixels down from the top left corner of the screen, which is off camera. You can hit Ctrl+F4 in game and check the slice collection editor to look at where it is. You need to convert the two into the same units of measurement, probably using one of the specia slice lookup codes to position your slice relative to the walkabout slice that represents the visible manifestation of the hero.
Liquid Metal King Slime
Send private message
 
 PostFri Dec 19, 2014 8:32 pm
Send private message Reply with quote
Oh yes, what giz said, not me.
Liquid Metal King Slime
Send private message
 
 PostFri Dec 19, 2014 10:55 pm
Send private message Reply with quote
Code:

set parent(sl, lookup slice(sl:walkabout layer))


This command will reparent the bullet so that instead of being attached to the screen, it will be attached to the map. Then its coordinates will be equivalent to the hero's pixel coordinates
Liquid Metal Slime
Send private message
 
 PostSat Dec 20, 2014 6:28 pm
Send private message Reply with quote
Bob the Hamster wrote:
Code:

set parent(sl, lookup slice(sl:walkabout layer))


This command will reparent the bullet so that instead of being attached to the screen, it will be attached to the map. Then its coordinates will be equivalent to the hero's pixel coordinates


Thanks that seems to work great and will also help me with the foreseeable problem off making the bullet pass behind overlays.
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
⍠ C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
Liquid Metal Slime
Send private message
 
 PostSat Dec 20, 2014 6:32 pm
Send private message Reply with quote
Spoonweaver wrote:
As far as making your code allow only one bullet at a time. That's so simple it's startling you'd ask. You set a tag or variable on when the bullet is there and off when it disappears. While having the variable/tag control whether the player can shot or not.


Oddly enough I think that's exactly what I'm doing but it's still allowing me to have multiple instances of the bullet at a give time. Here's the current code in full:

Code:

    variable(collection, sl, isShooting)
    if(key is pressed(key:X)) then(
        if(isShooting <> 1) then(
          isShooting := 1
          if(hero direction == right) then(
            collection := load slice collection(10)
            sl := lookup slice(sli:Bullet1, collection)
            set parent(sl, lookup slice(sl:walkabout layer))
            put slice (sl, hero pixel X (me), hero pixel Y (me))
            set slice velocity x (sl, 20, 10)
            set slice velocity y (sl, 0, 10)
            isShooting := 1
            wait for slice(sl)
            free slice(sl)
            isShooting := 0
            exit script
          )
          if(hero direction == left) then(
            collection := load slice collection(10)
            sl := lookup slice(sli:Bullet1, collection)
            set parent(sl, lookup slice(sl:walkabout layer))
            put slice (sl, hero pixel X (me), hero pixel Y (me))
            set slice velocity x (sl, -20, 10)
            set slice velocity y (sl, 0, 10)
            wait for slice(sl)
            free slice(sl)
            isShooting := 0
            exit script
          )
          if(hero direction == up) then(
            collection := load slice collection(10)
            sl := lookup slice(sli:Bullet1, collection)
            set parent(sl, lookup slice(sl:walkabout layer))
            put slice (sl, hero pixel X (me), hero pixel Y (me))
            set slice velocity x (sl, 0, 10)
            set slice velocity y (sl, -20, 10)
            wait for slice(sl)
            free slice(sl)
            isShooting := 0
            exit script
          )
          if(hero direction == down) then(
            collection := load slice collection(10)
            sl := lookup slice(sli:Bullet1, collection)
            set parent(sl, lookup slice(sl:walkabout layer))
            put slice (sl, hero pixel X (me), hero pixel Y (me))
            set slice velocity x (sl, 0, 10)
            set slice velocity y (sl, 20, 10)
            wait for slice(sl)
            free slice(sl)
            isShooting := 0
            exit script
          )
          isShooting := 0
        )
    )


I also noticed the wait commands in this script interfere with my LOS script causing my player to remain unseen by the guards while shooting. Perhaps I should run the bullets on timers or something...
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
⍠ C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
Liquid Metal King Slime
Send private message
 
 PostSat Dec 20, 2014 6:39 pm
Send private message Reply with quote
Set isShooting as a global variable not a local one like you have here.
Liquid Metal Slime
Send private message
 
 PostSat Dec 20, 2014 6:45 pm
Send private message Reply with quote
Spoonweaver wrote:
Set isShooting as a global variable not a local one like you have here.


Wow I'm so silly. Thanks now it works great. No need for timers either.
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
⍠ C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
Metal King Slime
Send private message
 
 PostSun Dec 21, 2014 1:31 am
Send private message Reply with quote
sheamkennedy wrote:
Spoonweaver wrote:
As far as making your code allow only one bullet at a time. That's so simple it's startling you'd ask. You set a tag or variable on when the bullet is there and off when it disappears. While having the variable/tag control whether the player can shot or not.


Oddly enough I think that's exactly what I'm doing but it's still allowing me to have multiple instances of the bullet at a give time. Here's the current code in full:

Code:

    variable(collection, sl, isShooting)
    if(key is pressed(key:X)) then(
        if(isShooting <> 1) then(
          isShooting := 1
          if(hero direction == right) then(
            collection := load slice collection(10)
            sl := lookup slice(sli:Bullet1, collection)
            set parent(sl, lookup slice(sl:walkabout layer))
            put slice (sl, hero pixel X (me), hero pixel Y (me))
            set slice velocity x (sl, 20, 10)
            set slice velocity y (sl, 0, 10)
            isShooting := 1
            wait for slice(sl)
            free slice(sl)
            isShooting := 0
            exit script
          )
          if(hero direction == left) then(
            collection := load slice collection(10)
            sl := lookup slice(sli:Bullet1, collection)
            set parent(sl, lookup slice(sl:walkabout layer))
            put slice (sl, hero pixel X (me), hero pixel Y (me))
            set slice velocity x (sl, -20, 10)
            set slice velocity y (sl, 0, 10)
            wait for slice(sl)
            free slice(sl)
            isShooting := 0
            exit script
          )
          if(hero direction == up) then(
            collection := load slice collection(10)
            sl := lookup slice(sli:Bullet1, collection)
            set parent(sl, lookup slice(sl:walkabout layer))
            put slice (sl, hero pixel X (me), hero pixel Y (me))
            set slice velocity x (sl, 0, 10)
            set slice velocity y (sl, -20, 10)
            wait for slice(sl)
            free slice(sl)
            isShooting := 0
            exit script
          )
          if(hero direction == down) then(
            collection := load slice collection(10)
            sl := lookup slice(sli:Bullet1, collection)
            set parent(sl, lookup slice(sl:walkabout layer))
            put slice (sl, hero pixel X (me), hero pixel Y (me))
            set slice velocity x (sl, 0, 10)
            set slice velocity y (sl, 20, 10)
            wait for slice(sl)
            free slice(sl)
            isShooting := 0
            exit script
          )
          isShooting := 0
        )
    )


I also noticed the wait commands in this script interfere with my LOS script causing my player to remain unseen by the guards while shooting. Perhaps I should run the bullets on timers or something...


You should be careful copy-pasting like that. True, it's faster in the short term to just copy paste the commands, but that means if you mess up or change your mind later, you have to fix everything multiple times. What you could do instead is something like...

Code:

#Stick these in as global variables somewhere and make sure to define
#'em at the start of the game.
ShotTicks := 10
ShotSpeed := 20

...

#Then for all of your Ifs do something like this
if (HeroDirection == Up)
then (
  TakeShot (HeroPixelX(me),HeroPixelY(me),0,0 -- ShotSpeed,ShotTicks)
  )

...

#and then have another script later where you define arguments to do
#the hard part..

script,TakeShot,WhoseX,WhoseY,XVel,YVel,Ticks,begin
#Make you actually define Collection and Sl, either as local variables or
#globals. Dunno how you're using 'em, but shouldn't matter.
collection := load slice collection(10)
sl := lookup slice(sli:Bullet1, collection)
set parent(sl, lookup slice(sl:walkabout layer))
put slice (sl,WhoseX,WhoseY)
set slice velocity x (sl, XVel, Ticks)
set slice velocity y (sl, YVel, Ticks)
wait for slice(sl)
free slice(sl)
isShooting := 0
end


The upshot to this is that if you want to change the way shots work later, you only need to change a couple of values and don't have to hunt down everywhere you used it. The script that checks your keypresses isn't going to have all of the slice moving particulars in it, which makes it neater and easier to parse when you're looking at it later, and it also means that if you decide later on to let enemies shoot, or a second player or whatever, that you can use that same script for all your shooting needs, just change the arguments a little.

I always hated getting advice like this, so I'm sorry and I still don't always do it the right way, but long term it's a good habit to get into.
Display posts from previous: