Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Reusing Tiles

Author
Message
chfire
16
Years of Service
User Offline
Joined: 22nd Aug 2008
Location:
Posted: 23rd Oct 2017 22:14
Hi Everyone

I am trying to populate a screen with 32x32 tiles, there's got to be a better way.
here is what I am using
SetSpritePosition(1,0,0)
CloneSprite(2,1)
SetSpritePosition(2,32,0)
CloneSprite(3,1)
SetSpritePosition(3,64,0)
Assuming i need to have the program place the sprites with 32 x,y increments I am not sure how to do this. could use
a nudge in the correct direction. I know there is a tile editor but sure i would get " lost " using that.
and would like to learn this from scratch.
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 23rd Oct 2017 23:08
you can use a for next loop
for x = 0 to GetVirtualWidth() Step 32
and drawsprite
just set the image you need before draw.
you can read image numbers from ascii file into an array or something else.
AGK (Steam) V2017.09.25 : Windows 10 Pro 64 Bit : AMD (17.9.3) Radeon R7 265 : Mac mini OS High Sierra (10.13)
chfire
16
Years of Service
User Offline
Joined: 22nd Aug 2008
Location:
Posted: 23rd Oct 2017 23:33
Thanks for the reply! I will give that a try.
sorry I posted this in the wrong forum.
chfire
16
Years of Service
User Offline
Joined: 22nd Aug 2008
Location:
Posted: 24th Oct 2017 00:31 Edited at: 24th Oct 2017 02:14
doing something wrong, But im not surprised. think I need to have it draw " drawsprite(1) from x to y " just not sure how it works.

PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 24th Oct 2017 06:29
This should get you started

Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 24th Oct 2017 07:33
sorry, it was incompleate , PartTimeCoder mentioned SetSpritePosition.
AGK (Steam) V2017.09.25 : Windows 10 Pro 64 Bit : AMD (17.9.3) Radeon R7 265 : Mac mini OS High Sierra (10.13)
chfire
16
Years of Service
User Offline
Joined: 22nd Aug 2008
Location:
Posted: 24th Oct 2017 21:56 Edited at: 25th Oct 2017 01:12
Thank you, that worked just fine! A few more questions.
I am using a sprite sheet with 127 " frames " is there a way to parse the .png and sort the individual animation? Like frame's 1 to 5=walk, 6 to 11= run ect.. the frames are 64x128.
I was thinking PlaySprite (png,30,0,fromframe,toframe) I would need to do this for every animation?

Right now the tiles are working fine, but having teething pains just trying to get player moment with the keyboard. any help with this would be much appreciated. sorry about the spaghetti code just trying to learn this..
Update: got player movment. not very smooth, but works. still can get the player animated with key getrawkeystate.

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 25th Oct 2017 13:19
If you're feeling adventurous, you can search for my TMX importer (get the most recent version) and dig through my code for study. You'll learn everything you'd ever want to know about tile maps and animated tiles. I can't recall how well I commented, but the code is pretty well structured though even still it may be a bit daunting for a beginner to look at. But I'll try to answer your question more directly.

Using your sprite sheet, create a single animated sprite. This is your base sprite. I like to hide the base sprite and never use it for anything being displayed to avoid any possible issues. You then clone your entire map from that single sprite. And all you do is set the sprite's frame to pick which tile is shown. Animated sprites don't have to actually animate anything, which makes them perfect for building tile maps from a single sprite sheet.

For various animation sequences, like run, walk, or jump, yes you would need to set up the range for the frames for every sequence. But this is really easy if you plan it out and make good use of functions and a couple constants.

This is how I do it:



Not 100% sure on my syntax, haven't used agk for awhile. When you switch animations, you need to set the sprite's frame otherwise you can end up with a delayed effect due to the previous animation waiting to complete before it starts the new one. And then you can either call play sprite from the function itself, or manage it elsewhere. For instance, only animate while a button is pressed.

If no one else chimes in by the end of the day, I'll write a real example for character animation when I get home.


"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 25th Oct 2017 19:21 Edited at: 25th Oct 2017 19:22
Quote: "I am using a sprite sheet with 127 " frames " is there a way to parse the .png and sort the individual animation? Like frame's 1 to 5=walk, 6 to 11= run ect.. the frames are 64x128."

Yes.
Click on the "SpriteSheet XML Control" link in my signature
chfire
16
Years of Service
User Offline
Joined: 22nd Aug 2008
Location:
Posted: 25th Oct 2017 22:57
Thanks for helping everyone! Phaelax, Scraggle they look like some useful programs. hopefully I will understand coding enough to make use of them one day.
for now I am just past the " hello world " stage of programing. Don't want to copy & paste code I don't understand. right now I am trying to get the animation
and movement with this code. have been searching, think I will figure it out.. might take a few day's lol.


chfire
16
Years of Service
User Offline
Joined: 22nd Aug 2008
Location:
Posted: 26th Oct 2017 00:28
Figure it out, don't know if it's 100% correct but it works. now to scroll the screen with "player" locked to the center of the screen.
thanks again everyone. next time I will make sure to post my questions in the newcomers corner.

puzzler2018
User Banned
Posted: 26th Oct 2017 00:34
welcome to agk, its a wonderful language
puzzler2018
User Banned
Posted: 26th Oct 2017 00:36
are you a programmer? or learner recently?
chfire
16
Years of Service
User Offline
Joined: 22nd Aug 2008
Location:
Posted: 26th Oct 2017 01:36 Edited at: 26th Oct 2017 02:16
just learning, I have a bad habit of getting stuck coding and giving up for awhile.. if I make a little progress it keeps me motivated.
I did make a little progress but, this works with the " a and d" keys and animation is correct but, it's drawing 2 sprites on screen one for each direction.



chfire
16
Years of Service
User Offline
Joined: 22nd Aug 2008
Location:
Posted: 26th Oct 2017 02:01 Edited at: 26th Oct 2017 02:22
no option to delete posts?
I am creating 2 sprites for animation just don't need both sprites on screen at the same time. was thinking " SetSpriteVisible " but don't think thats correct.
do I need to setup a " player " and set the sprites to the player?
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 27th Oct 2017 23:03
You're positioning two sprites at the same coordinates. I'm guessing you have a different sprite for facing each direction? If so, that's not necessary.


"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
chfire
16
Years of Service
User Offline
Joined: 22nd Aug 2008
Location:
Posted: 28th Oct 2017 02:14 Edited at: 28th Oct 2017 02:31
Yes, after some time I have concluded I don't understand when and were I need to update the " players " coordinates
also I am using 4 frames animation to walk left/right with the A and D keys. 8 in total. so my problem is I have 2 animations for left/right using playsprite.
if I hold down the A key both " sprite/animations "s A and B are on screen at the same time.

So I could create 4 animation's for up/down/left/right and the would all be on screen at the same time.
I just want to use the w,a,s,d keys to move the player and have the appropriate animations..

Here is the full code I am using. I can see the position error's just don't understand it. I Feel I need to assign all the animation's to the " player " just not sure how to set that up

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 28th Oct 2017 04:39
If you want to keep the player centered, you never change his coordinates. You change the rest of the map tiles.

Can you attach the sprite you're using, I'd like to play around with it. Glancing at your code, I see you creating two sprites, both using the hero image. (lines 19-20) I'm guessing you meant to make one use the ground?



"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 28th Oct 2017 05:56
I've made a small example of what I think it is you're after. Did my best to comment each piece and included the media I used. I'd set up things a little differently myself, but I didn't want to over-complicate the example. I actually wrote my own custom animation functions which provide a smoother feel, but that in itself is a whole other project.



"I like offending people, because I think people who get offended should be offended." - Linus Torvalds

Attachments

Login to view attachments
chfire
16
Years of Service
User Offline
Joined: 22nd Aug 2008
Location:
Posted: 28th Oct 2017 17:58 Edited at: 28th Oct 2017 17:59
Hi Pahelax, Thanks for the code! I understand about 50% of it lol. a few questions regarding animation

1. " Global _animations as integer[4,2] " im Assuming 4 is the 4 walk animations but what is the 2?
2. #CONSTANT VK_DOWN 40, #CONSTANT VK_UP ect.. 38 Assuming these are the scan codes and will not change in the program?
3. setSpriteAnimation(spr_player, 100, 100, 32) Assuming 32 is the frame count in " player.png " 100,100 in not the image size. so I dont know what this is for? coordinates?
4. _animations[ANI_WALK_RIGHT,1] = 17 ect.. I understand these are the walk animations but what do the = numbers do?
5. if getRawKeyState(VK_RIGHT) dec mapX, 3 is this to move the map 3 degrees in the x when the key is pressed?
6. function setPlayerAnimation(sprite, animation)
playSprite(sprite, 10, 1, _animations[animation,1], _animations[animation, 2])
endfunction

not understanding this.

7.if getRawKeyState(VK_UP)
inc mapY, 3
if lastDirection <> VK_UP then setPlayerAnimation(spr_player, ANI_WALK_UP)
moving = 1
not sure about " inc mapY, 3 " and " if lastDirection <> VK_UP " and " moving = 1 "

sorry for all the questions, and thanks for your help. I know enough to copy and paste some of your code and make it work, but I would not understand it all.
so I don't want to do that.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 30th Oct 2017 21:15
Quote: "1. " Global _animations as integer[4,2] " im Assuming 4 is the 4 walk animations but what is the 2?"

Quote: "4. _animations[ANI_WALK_RIGHT,1] = 17 ect.. I understand these are the walk animations but what do the = numbers do?"

The 2 is for the 2 dimensions which store the starting and ending frame number for each sequence of animation.

_animations[ANI_WALK_RIGHT,1] = 17
_animations[ANI_WALK_RIGHT,2] = 23

So for the "walking right" animation, the frames go from 17 to 23. Do you need to use constants for this? No, but it makes it easier as far as readability.

Quote: "#CONSTANT VK_DOWN 40, #CONSTANT VK_UP ect.. 38 Assuming these are the scan codes and will not change in the program?"

Correct. Again, using constants here is simply for readability. It's easier to see what VK_DOWN means rather simply 40. (unless you have a superb memory of scancodes, most of us never remember them)

Quote: "3. setSpriteAnimation(spr_player, 100, 100, 32) Assuming 32 is the frame count in " player.png " 100,100 in not the image size. so I dont know what this is for? coordinates?"

This is easily answered in the help file. You're correct, 32 is the frame count in the sprite sheet. But how big is each frame? It's 100x100.

Quote: "5. if getRawKeyState(VK_RIGHT) dec mapX, 3 is this to move the map 3 degrees in the x when the key is pressed?"

Not degrees, but pixels. It'll shift the map in that direction by 3px. You can further expand on this by setting up boundaries so you can't scroll beyond the end of the map.

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 31st Oct 2017 03:24
I was getting ready to leave work and didn't have time to finish answering all your questions. So here is part 2.


Quote: "6. function setPlayerAnimation(sprite, animation)
playSprite(sprite, 10, 1, _animations[animation,1], _animations[animation, 2])
endfunction
"


What don't you understand? It just simplifies the playSprite function. You just pass the sprite id (of the character) and the constant of whatever animation sequence you want. When reading code it's easier to decipher and understand at a glance than writing out the array name "_animations" each time.


Ok, let me explain the need for "lastDirection". We want the player to animate only when it's moving, yes? So, when the player presses a direction we play that specific animation; such as left, right, up, or down. Now here's the tricky part. We use getRawKeyState instead of getRawKeyPressed because it will return true for as long as the key is held down. But we don't want to call playSprite the whole time, otherwise the sprite will never make it past the first frame because it'll constantly be reset to the beginning of its frame sequence. What we want to do is only call playSprite once whenever a direction changes.

So first we check the key states. If moving RIGHT, then play right animation. (followed by the other IF commands for the other directions) After that, we capture the last key pressed and store it as lastDirection. When the program loops back to the beginning, it'll see we're still pressing right and the last direction we pressed was also right. Therefore, no need to update the animation sequence. The last thing to understand is the "moving" flag. This flag is reset every frame, before we check the key controls. This is important because "moving" will be set to 1 for as long as a key is pressed. If we release a key, the next frame of the game loop resets it to 0. After the key controls check, if moving is still 0 then we need to stop the player animation. We also reset "lastDirection" at that same time as well, otherwise the animation will not resume if you're pressing RIGHT, stop, then immediately press RIGHT again. It would still see the last direction was the same and thus will not initialize the play command again.


This system does have minor bugs, such as pressing multiple directions at a time. It's easily fixed but I felt it was outside the scope of the example for now.

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds

Login to post a reply

Server time is: 2024-11-24 08:33:22
Your offset time is: 2024-11-24 08:33:22