It looks like you're new here. If you want to get involved, click one of these buttons!
I was forum digging and I discovered two ways to use sprite sheets that work but not with every sprite sheet.
https://codea.io/talk/discussion/comment/13909#Comment_13909
I can get the caveman sprite sheet to work both ways but not the Wizard.
Just wondering what I’m doing wrong.
The wizard image is 2000x200
Each frame is 200x200
10 frames in total
For Main
function setup()
local img = readImage(asset.documents.Dropbox["Photo Apr 09, 7 51 25 PM.png"])
wizard = Spritesheet(img,WIDTH,HEIGHT/2,10,1,200,200,0,10)
end
function draw()
background(40, 40, 50)
wizard:draw()
end
function touched(touch)
wizard:touched(touch)
end
Spritesheet class
Spritesheet = class()
function Spritesheet:init(img,x,y,rows,cols,frameWidth,frameHeight,frame,totalFrames)
self.x = x
self.y = y
self.frame = frame
self.totalFrames=totalFrames
self.frameWidth = frameWidth
self.frameHeight = frameHeight
self.frameRows = rows
self.frameCols = cols
self.frameSize = vec2(self.frameWidth,self.frameHeight)
self.counter = 0
self.mesh = mesh()
--self.texture = img
self.mesh.texture = img -- self.texture
self.mesh:addRect(0,0,self.frameWidth,self.frameHeight)
end
function Spritesheet:draw()
self.counter = self.counter + DeltaTime
if self.counter > 1/30 then
self.frame = self.frame + 1
if self.frame > self.totalFrames then
self.frame=0
end
self.x = self.x -1
colUnit = 1/self.frameCols
rowUnit = 1/self.frameRows
row = math.floor(self.frame / self.frameRows)
col = math.floor(self.frame % self.frameCols)
self.mesh:setRectTex(1,
col * colUnit,
( 1-rowUnit) - row * rowUnit,
colUnit,
rowUnit)
self.mesh:setRect(1,self.x,self.y,self.frameWidth,self.frameHeight)
self.counter = 0
end
self.mesh:draw()
end
function Spritesheet:touched(touch)
end
Comments
Here is way 2 working with caveman but I can’t get to work with wizard.
Caveman download above
@Jarc Here’s a version that works with both. Enter the number of images wide and high (4,4 for caveman and 10,1 for wizzard) then press extract. You can slide the scale to increase or decrease the size and slide your finger on the screen to move the image around.
@dave1707 Your version works the best and I’ve been digging through the forums all day. Thanks!
@Jarc It only works if the sprite sheet is created right and not every sprite sheet is.
@dave1707 is it possible to change animation by touching a specific point on the screen. Meaning if I have a on-screen D-Pad or joystick if I touch Down the code will run sprite sheet row 2, 1-8.
Example sprite-sheet attached.
Maybe a better example with a visual. Basically I’m trying to link the sprite sheet rows to each direction of the joystick or Dpad controller.
The function extract creates a table of each image. You just need to loop thru each section of the table corresponding on the direction your going. For example, if your going down, loop thru table positions 9 thru16, if up then 49 thru 56.
@dave1707 So loop through the table with a if-then statement in touch function?
It’s not going to be as easy as it sounds. The circle might show the eight directions, but the joystick does every direction, so an if statement in touch isn’t going to do it. I’ll look into it more tomorrow.
@Jarc Here’s a hacked up version. Change the img name to the name you have for the person sprite sheet. The joystick is now eight squares that you touch to change direction. Changes can be made from this hacked code.
@dave1707 Awesome, thanks. Great code to start with. I’ll be making changes and working on this tonight.
@dave1707 I got the Up,Down,Left and Right to work but I need help with the diagonals.
@Jarc Here’s another version to work from.
@dave1707 Amazing! As always, thank you for the help.
@Jarc Here’s a new version. I stripped all the unnecessary code out and created separate functions for routines. This might be easier to modify.
@dave1707 This is how I’m modifying to fit specific backgrounds and how I’m increasing character speed. Is this the easiest way with this code?
Background example attached.
On a side note, anyway to go diagonal using vec4? I used it to go Up, Down, Left and Right but can’t figure out diagonals that way.
@dave1707 in the code above I only made changes here.
@Jarc You can add whatever background you want. You can add a table that has different values in it and depending on the table value you can insert different sections in the ground. You can then avoid them as the person walks around.
Why do you want to add a vec4 for movement. The movement is working in all directions now.
@Jarc Here’s an example of a background I have in some other program I created. The player can collect the stars by running into them. They can’t go up on a black area, but they can fall thru a black area. They also can’t go thru a brick area.
@dave1707 I wanted to add a vec4 for movement just for the sake of learning and because I couldn’t figure it out. I’m going to use your way though, especially now since I know how to add backgrounds with the table. The visual helped me comprehend. Thanks!
@dave1707 I thought I knew what you meant but after a couple of hours of trail and error I realized I don’t. Can I get a example in code of inserting different sections in the ground or a link to help me understand. All I’m getting is the background image in all directions endlessly.
Slide your finger to move the ground around.
@dave1707 Thanks btw I figured out how to move all directions using vec4.
Correct if I’m wrong but to my understanding your way moves the ground corresponding to the sprite sheet image.
I believe this way moves the spritesheet image via x y coordinates.
@Jarc What are you going to do when the sprite person walks off the edge of the screen. Your vec4 works, but look at the size of your touched routine. The purpose of a class is to cut down on the code for multiple items. Using a class, you make changes to the code for 1 button (which is all buttons), in your code you’ll have to make changes for 8 buttons. As you code more and use classes, you see the advantages of using them. It took me a long time before I understood how classes worked and why they should be used.
PS. By moving the ground, the ground can be a lot larger than the screen size. In the background example above, as you scroll the background around, you’ll see that it’s a lot larger than the screen.
@dave1707 Way to shoot me down Dave, just kidding I see your point. Thanks for the insight you’re right.
@Jarc I didn’t mean anything by what I said, just trying to point out things. By moving the character, you’re kind of limited by the screen size unless you add a lot of code when the character reaches the screen edge. By keeping the character in the middle and moving the background, the background can be any size. You can constantly create more background off screen as the character moves. As for vec4’s, it’s good to learn and try new things. One thing you’ll find that you use a lot are tables and classes. You can skip using them, but you’ll be writing a lot more code and it will be more complicated. Like I said above, it took me a long time before I understood classes. I didn’t know why they were used and I didn’t have anyone to explain well enough their use. Or maybe I was told enough, but I just didn’t understand. Just keep trying new things and asking questions.
@dave1707 how do I tie a dynamic collider to the sprite animation so whenever I move any direction it stays on him.
2nd question, how do I set up a static collider so he can’t walk through walls and can interact with objects.
@Jarc You know the x,y position of your sprite walker. To prevent the walker from going thru a wall, just make sure the walker won’t have the same x,y position plus or minus some value as the wall.
@dave1707 Why is the other ellipse moving relative to me? I want to be able to walk up to it, is it a problem with background? Also when I give the sprite collider coordinates
it doesn’t work.
Here’s what I have so far.
@dave1707 I made this change but the other ellipse still moves relative to me.
Both of your collider ellipses need to be relative to the ground, dx,dy.
@dave1707 but the collider isn’t tied to him anymore.
@dave1707 The one with the comment “sprite collision circle” and “Collider on sprite” beside it
Is this ok.
@dave1707 This way ties it to him and I can walk to other ellipse but collision stops working.
@dave1707 same problem with what I sent. It ties it to him and I can walk to other ellipse but collision isn’t working now.
.
.
Collisions don’t work right if you try to override it. If the objects aren’t free to move on their own, you’ll get a collision detected but it won’t stop a manually moving object.
@dave1707 So for my question at 11:00am I would have rather you said “it can’t be done on Codea” then having me go down a rabbit hole.
@Jarc The collision still happens, but since you’re controlling the person, you’ll have to code for the collision. If a collision is detected, then don’t allow any motion in the current direction.
@dave1707 Sorry to bring this up again but I’m having a hard time understanding what you’re saying. I think the physics engine in Codea needs tweaking. The collision isn’t happening because my collision detector function would tell me. Here is a more simple example(project below). The sprite I don’t have the Dpad linked to collision works on everything but as soon as I link the Joystick to a sprite or collision body the collision only works on falling objects. I got this method from box2D physics simulator engine so not sure why it isn’t working here when physics principles are the same. The sprite I’m controlling shouldn’t be able to walk through the edge line.