Moving slices to make an attack.

Ask and answer questions about making games and related topics. Unrelated topics go in that other forum.

Moderators: marionline, SDHawk

Post Reply
User avatar
dantedynamite
Red Slime
Posts: 44
Joined: Fri Dec 19, 2014 6:37 am

Moving slices to make an attack.

Post by dantedynamite »

Ok, so I'm playing with the script for a dungeon crawler of sorts where your character has a bow and shoots an arrow in a certain direction based on if you press W, A, S or D for the directions up, left, down, and right respectively and move with the arrow keys. The point here is to try to make a sort of Legend of Zelda type of arrow shooting. I've also made a sort of fog of war that is revealed once you explore a certain area on the map. I'm not very familiar with slices at all, so excuse me if I am just overlooking something totally elementary. Here is the code:

Code: Select all

include, plotscr.hsd
include, ShowMe.hsi 
include, Scancode.hsi

#----------------------------------------------------------------------------

global variable, begin
	1,  cx
	2, cy 
	3, attackcol
	4, attacksl
	5, playing
end

#---------------------------------------------------------------------------- 

plotscript, main, begin                       #set as new game script 
runalways
end

#----------------------------------------------------------------------------
plotscript, atk, begin

if (key is pressed(key:w)) then (

set slice velocity y (sli:Attack,10)
show value (0)                                 #debugging purposes 
)

if (key is pressed(key:s)) then (
set slice velocity y (sli:Attack,-10)
show value (1)
)

if (key is pressed(key:d)) then (
set slice velocity x (sli:Attack,10)
show value (2)
)

if (key is pressed(key:a)) then (
set slice velocity x (sli:Attack,-10)
show value (4)
)

end

#----------------------------------------------------------------------------

plotscript, fog,begin                                #"reveals" all of the blocks 
write map block (herox--1, heroy+3, 0, 1) #1		 #relative to the hero. Layer
write map block (herox, heroy+3, 0, 1) #2			 #1 has to be covered in "shadow"
write map block (herox+1, heroy+3, 0, 1) #3			 #which is just black.
write map block (herox--2, heroy+2, 0, 1) #4 
write map block (herox--1, heroy+2, 0, 1) #5
write map block (herox, heroy+2, 0, 1) #6
write map block (herox+1, heroy+2, 0, 1)  #7
write map block (herox+2, heroy+2, 0, 1) #8
write map block (herox--1, heroy+1, 0, 1) #9
write map block (herox--3, heroy--1, 0, 1) #23
write map block (herox--2, heroy+1, 0, 1) #10
write map block (herox--1, heroy+1, 0, 1) #11
write map block (herox, heroy+1, 0, 1) #12
write map block (herox+1, heroy+1, 0, 1) #13
write map block (herox+2, heroy+1, 0, 1) #14
write map block (herox+3, heroy+1, 0, 1) #15
write map block (herox--3, heroy, 0, 1) # 16
write map block (herox--2, heroy, 0, 1) #17
write map block (herox--1, heroy, 0, 1) #18
write map block (herox, heroy, 0, 1) #19
write map block (herox+1, heroy, 0, 1) #20
write map block (herox+2, heroy, 0, 1) #21
write map block (herox+3, heroy, 0, 1) #22
write map block (herox--3, heroy+1, 0, 1) #23
write map block (herox--2, heroy--1, 0, 1) #24
write map block (herox--1, heroy--1, 0, 1) #25
write map block (herox, heroy--1, 0, 1) #26
write map block (herox+1, heroy--1, 0, 1) #27
write map block (herox+2, heroy--1, 0, 1) #28
write map block (herox+3, heroy--1, 0, 1) #29
write map block (herox--2, heroy--2, 0, 1) #30
write map block (herox--1, heroy--2, 0, 1) #31
write map block (herox, heroy--2, 0, 1) #32
write map block (herox+1, heroy--2, 0, 1) #33
write map block (herox+2, heroy--2, 0, 1) #34
write map block (herox--1, heroy--3, 0, 1) #35
write map block (herox, heroy--3, 0, 1) #36
write map block (herox+1, heroy--3, 0, 1) #37
end


#----------------------------------------------------------------------------

plotscript, runalways, begin
attackcol := load slice collection (1)
attacksl := lookup slice (sli:Attack, attackcol)
playing := true 
while(playing == true) do(
fog
atk
wait(1)
)
end

 


What am I doing wrong? It keeps coming up with the error
"setslicevelocityx: slice handle 4 has already been deleted" and
"slicex: slice handle 4 has already been deleted" and
"slicescreenx: slice handle 4 has already been deleted" and
"setslicevelocityy: slice handle 4 has already been deleted" and
"slicey: slice handle 4 has already been deleted" and
"slicescreeny: slice handle 4 has already been deleted" and
when any of the WASD keys are pressed.

The fog script works just fine though.


I should note that in the "attack" is a sprite in slice collection 1 and it has the lookup code "4 Attack"

The idea was that if a slice is used "slice collide (handle1, handle2)" where handle1 is the attack and handle2 is an enemy onscreen which is just an npc it will destroy that NPC, but again, I'm not too sure about how to do that because I haven't figured out slices.

I could do that, or I suppose, if you guys have any suggestions on how to do that in an easier way, I would surly do that indeed.

What do you guys have to say?
Any comment is appreciated.
User avatar
sheamkennedy
Liquid Metal Slime
Posts: 1110
Joined: Mon Sep 16, 2013 9:29 pm
Location: Tama-shi, Tokyo, Japan
Contact:

Post by sheamkennedy »

You have to load in the slice collection first that you are using. Then you can use codes to manipulate those slices in the collection. Since the slices aren't loaded in to your game then you will get errors saying they don't exist and stuff like that.

Here's an example of what to do:

Code: Select all

variable(collection)
if(key is pressed(key:D)) then(
           #This loads the collection
            collection := load slice collection(8) 
           #This looks up said bullet in the collection
            Bigshot2 := lookup slice(sli:Bigshot2, collection)
            #This will parent bullet to hero so that the bullet is bound to the hero
            #or in my case it's an NPC which I wanted the bullet to originate
            #from
            set parent(Bigshot2, get NPC slice (3)) 
            # this aligns the bullet however you want
            set slice edge x (Bigshot2, edge:top, 0) 
            set slice edge y (Bigshot2, edge:top, -15) 
            # this sets bullet velocity however you want
            set slice velocity x (Bigshot2, -17, 10) 
            set slice velocity y (Bigshot2, 0, 10)
          #.... etc
          )
Hope that helps you set it up better. Once you make a bullet shoot you will likely want it to be destroyed if it doesnt hit an enemy within a few seconds. For this you'll need a timer probably, or walls that destroy the slice on collision.

In addition you'll need to do a collision check to see if the bullet collides with enemy. This I know a bit about too since Ive done it for a few games. But let's focus on one chunk at a time.

EDIT: Oh my bad, I didn't see that you had that at the bottom of your code... Well I'll leave this here. It may come in handy for other stuff. It's good to see other peoples working codes than manipulate them for your own purpose.
Last edited by sheamkennedy on Thu Mar 05, 2015 4:42 am, edited 3 times in total.
⊕ 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
User avatar
Gizmog
Metal King Slime
Posts: 2615
Joined: Tue Feb 19, 2008 5:41 am

Post by Gizmog »

I think the issue is you're mixing up Slice Handles and Slice Look-Up Codes. When you write...

Code: Select all

set slice velocity x (sli:Attack,10)
Sli:Attack is generally the format for a slice Look Up code. Set Slice Velocity X is asking for a slice handle. What you'd want to write is

Code: Select all

setslicevelocity x (lookupslice (Sli:attack),10)
or better yet

Code: Select all

variable (ThisAttack)
setvariable (ThisAttack,lookupslice (Sli:Attack))
setslicevelocityx (ThisAttack,10)
So you can just type "ThisAttack" for each subsequent use of that slice in that script.... which actually, double checking, you're alrady doing in the RunAlways command! Try uhh...

Code: Select all

set slice velocity y (attacksl,-10)
because that's what

Code: Select all

attacksl := lookup slice (sli:Attack, attackcol)
is doing!

You've got your direction commands mixed up a little too:

Code: Select all

if (key is pressed(key:w)) then ( 

set slice velocity y (sli:Attack,10) 
show value (0)                                 #debugging purposes 
)

if (key is pressed(key:s)) then ( 
set slice velocity y (sli:Attack,-10) 
show value (1) 
)
You count X/Y values from up/left to down/right. So if you push W, for Up, your slice should have a negative velocity and S should be a positive velocity. The same for D and A.

Though looking at it again, there's no while loop and I don't know how you're calling this script so you might only be checking whether or not that key is pressed for one eighteenth of one specifc second.
User avatar
dantedynamite
Red Slime
Posts: 44
Joined: Fri Dec 19, 2014 6:37 am

Post by dantedynamite »

Wow! That actually clears up some very specific questions I've had. I'll see where this takes me.
TMC
Metal King Slime
Posts: 4101
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

In order to check for collision of the arrow slice with a specific NPC you can write "slice collide(attacksl, get npc slice(npc ref))". However this will check whether the NPC slice and the attack overlap by even a single pixel, which is probably far too wide. So what you can do is create a container slice in the slice collection which is the size of the hitbox (for example, 4x10 pixels if shooting an arrow up), give the container the "Attack" lookup code, and attach the actual sprite to that container.

You'll very probably want to parent the attack slice to a map layer so that it goes behind overhead tile layers and so its position is in map coordinates instead of screen coordinates.
Post Reply