We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, I'm trying to use some sprites in Processing, I already have class wich use only one image but I'd like to change that to multiple ones.
My classes look like that https://paste2.org/0Z7tgG1E with others classes that extend Vaisseau
Answers
@poisson86 --
I'm not sure I understand what your question is.
Are you saying that you want to change your class from one image and one mask:
...to multiple images and multiple masks?
Keep in mind that there are also sprite libraries for Processing if you want to pass around a sprite object rather than an image list -- check out the Libraries listed under Animation, like Sprites and Frames.
Also, keep in mind that many sprites are not implemented as multiple images at all -- instead they implement pointers to different offset regions on a single image (a "sprite sheet").
@jeremydouglass
Yes I know they're librairies but I don't how to use them quite well, and what I want to do is a bit blury too.
Entity(int vie, float x, float y, PImage[] imlist, PImage[] masquelist, boolean ally)
It could be something like that yes i thinkUp ? :( I can't do proper animations in my program and it's kind of important :/
You have to decide which would be quicker, learn how to use an existing library like Sprites or learn how to do it from scratch.
Looking at the Vaisseau class you have a lot to do to get sprite animations.
Yes that's why I'm a bit lost :/ i already download and check out the Sprite librairie but I'm not using my class as in it
@poisson86
Check the following link: http://www.lagers.org.uk/s4p/ There is a reference an examples. If you are stuck, ask here in the forum. Notice the creator of this library is @quark... so you are a very lucky fellow!
Kf
@kfrajer @quark
Okay thanks :) So in the librairy I don't understand what
Sprite (PApplet theApplet, String imageFname, int cols, int rows, int zOrder)
theint zOrder
means and how do we use the.setFrameSequence();
Because I've done :
explosion = new Sprite(this, "Explosion.png", 7, 1, 100); explosion.setFrameSequence(1,8); explosion.setXY(250,250);
With this Sprite but it's not at the right size and it isn't "moving" image.noelshack.com/fichiers/2016/48/1480439880-explosion.png
First thing is that the image you are using is not suitable for sprites. It is common in games to use a sprites sheet, this is an image-file with multiple tiled images where ALL tiles are the same size. In your image the tiles are not the same size.
Here is a sprite sheet I downloaded of the internet and I have used it in the sketch shown below. Notice it has 25 frames and they are numbered left-to-right top-to-bottom starting with 0 (zero). So the frame numbers are 0-24 inclusive.
The zOrder attribute determines the order that sprites are drawn. For instance if you have a spaceship and explosion sprite you would want the explosion to appear 'over' the spaceship. So the explosion sprite should have a higher number than the spaceship. The actual zOrder values are unimportant they simply decide on the order sprites are drawn on the screen.
To animate the sprite we simply tell the sprite which frames to animate, the time interval between frames and how many times to repeat the animation.
So in my sketch we have
In the sketch below we have created a StopWatch object (line 9) and this will be used to measure the elapsed time between frames. The real power of Sprites can be seen in the
draw
method.Line 18: we make a note of the elapsed time in seconds.
Line 19: this single statement will update ALL sprites that have been created
Line 20: this single statement will display ALL sprites that have been created
Sprites is a powerful library which means that it takes a while getting used to but will do most of the hard work for you. You can use the sketch code below as a basis for trying out different parts of the library.
https://forum.Processing.org/two/discussion/19397/initializing-arrays-within-a-method#Item_4
@quark
Okay so I understood your example but now i'm trying to do this in a class but nothing appear :/
https://paste2.org/xEAMhsVf
@poisson86
We need to see all the code, or a short running version showing your problem. Your code in the link doesn't run as it is.
Kf
So I think I removed things as much as I can : https://paste2.org/K0hEwUaM
I am not going to try and incorporate the library into your code instead I am going to give you an example of how to effectively use the Sprite library. Then you can adapt your code to suit.
The first thing is that the base class should be the Sprite class. This gives your classes all the power of Sprite including easy updating and displaying. It also allows you to add extra functionality.
I have made your Entity class non-abstract because it now inherits from Sprite.
The update method in the child classes overrides the one in Sprite so we have to call the overridden method in Sprite first to make sure the sprite moves and is animated.
Hopefully you will manage to work out what the code is doing but if not ask here. :)
BTW the images used are below except explosion which is above. I leave you to open the Tardis doors ;)
@quark -- quick question. I see your sketch needs 5 pngs, but four are listed in this post. Is the fifth available somewhere? Or is the whole example sketch with resources somewhere?
Alright @quark
Your exemple perfectly shows how Sprite works but you see in my program i have x, y and so on which permit me to move the image right after. So, should I redo everything to get something like you or it isn't necessary ?
@jeremydouglass the fifth image is the explosion which I posted in an earlier comment.
Eventually I will include the code as an example sketch in the library download, but don't hold your breath as I am busy on a new project.
If you have your own variables like x and y for the sprite position then there will be confusion over which ones to use, the position stored in Sprite or the ones provided in your own class.
I would recommend that you redo everything. Not so bad really because the example shows you how to do it and the Sprite class provides so much functionality that you would have to create. After all it took me several months to create the library first in C++ and later more months to translate it into Java to be used with Processing.
Once you become familiar with the library you will be able to save so much time.
Alright thank you ;) i'll do that tomorrow, huge physics exam in the morning ^^
Good luck with the exam :)>-
Hi again :) so i'm going to try to redo everything but you saw that my class entity create objects in an arraylist. What should be the differences then ? Because you change x,y coordinate in your void setup() and i don't.
Also I don't think i'm capable to rewrite everything with your method, this took me so much time and redo it would take almost as much time.
In your code the x/y sprite position is setup in the constructor. The sprite library uses the constructor to set up the sprite image frames and provides the
setX
,setY
andsetXY
methods to do that.The library keeps track of all the sprites so that they can be updated and draw with 2 simple statements.
which must be inside the main draw method. You can remove a sprite so they are not updated and drawn by deregistering it with
S4P.deregisterSprite(sprite);
and can be addded back later with
S4P.registerSprite(sprite);
So you don't need your own arraylist of sprites for updating and drawing the sprites but you might want your own to simplify collision detection etc.
Notice that we don't update the sprites position manually rather we set the sprites velocity and let the library do the hard work.
No, i was talking about the rest, i already have my collision and everything but add Sprite would change too much things that I will be lost (like changing every Constructor)
If you think that using Sprite will cause too many problems then don't use it.
Ultimately its your choice. :)
I'm not saying that, it's just I don't have enough experience to change everything without messing it ^^
Make a copy of the sketch as it is now and try modifying the copy. If it doesn't work you still have the original.
The alternative is to start again from scratch using the Spite library, but I can understand your reluctance especially if you have already done a lot of code.
I have been posting on this forum since December 2008 when Processing was version 1.2.1. My first library, G4P was released in March 2009 and since then PS has morphed through PS1 > PS2 > PS3 and there is hardly any of the original code left and that's thousands of lines of code. So I understand where you are coming from. There were times when I could have quite happily abandoned G4P because that the changes I needed to implement were so massive.
I can't make the decision for you, you must do what you think best. :)
Alright :) I may at least try a bit :p Just a last question, do you have any clues change your entity as mine like using an arraylist for the objects ?
You appear to be using the arraylists to track all the entities that are alive and and dead so you can latter remove the dead ones without causing concurrent access violations. Which is a neat trick and one I use in Sprites, I have two lists of sprites to add and sprites to remove which are updated by the registerSprite and deregisterSprite, latter the updateSprites method will add the new sprites and remove the dead sprites from the main list.
So put the two arraylists back in
then when you create a sprite you can add it
when a sprite becomes dead remove it. This is the update method from the Bullet class
Afraid I have to go now I am working on a new library and an application based on it, but I will look in and help when I can.
Finally I won't use it sorry :/ it demands me to redo everything and could be even unappropriate with other things I have :/ I'll try to look for something else maybe more simple to use x)
No need to apologise, it was an option, you tried it, and it wasn't for you. :)
Thank you anyway :) if you have an other idea just to do an explosion (not animate something moving) I won't be far away :)
There is a lib to animate gifs
So if you have a gif with an explosion movie
@chrisir which one is it ? :) pls
@poisson86
Install the imageLoader library using the library manager. Then check the example FileGifLoaderExample
Other ideas... search in the forum:
https://forum.processing.org/two/search?Search=gif
https://forum.processing.org/two/search?Search=gifAnimation
Kf
you also need a gif of an explosion!!!
ok, you can do a nice explosion without sprites, just like a particle system:
you can also just use the sprite sheet with the explosion by quark (further up) to make your own animation of this sprite sheet.
Click mouse to explode.
Note that the sprite sheet by quark must be in the data folder with the name
P6LFNRLPISUT.png
(which occurs if you just save it from the forum).The data of the sprite sheet are very important: This is a 5x5 image sprite sheet with size of 64x64 per one image.
Note: When you want to have multiple explosions, store them in an
ArrayList
and work with that. (Or PM me).Best, Chrisir ;-)
Hi @Chrisir :) thank you for your help but with your code combine with mine (and some modification) the explosion never stop and loop at the same place no matter what I do :(
Are you referring to my 1st or 2nd version?
Post your code to get help
Your second version sorry ^^ this is the same code that I posted few days ago and I put the
explosion1.start(x, y);
in thedestroy(){}
function :)and by the way I would need multiple explosion (didn't see this part before ^^)
https://paste2.org/nXw2Z2AF
that is not the entire code
class Explosion is missing
missing ; here
how did you run the code in the first place?
woops forgot to past it ^^ but your Class is the same appart the name of the file ^^
next error :
can't find anything named explosion1
dTime cannot be resolved....................
cannot find S4P ..........................
do you want me to do your work for you?
Have you really ran the code?
https://paste2.org/ecX2F8f3 This one works :) I pasted a little bit too fast
And I have to test on my main program but I cannot paste it, because it's way too big so sometimes I fail my pastes sorry :/
you need this at beginning of draw()
Wow that worked :o can you explain why ? :) I'm going to try to add multiple explosion.
Thank you for help and your time :)
your way of removing stuff from ArrayList one by adding it to ArrayList toRemove is very clumsy
Basically, the list toRemove wasn't empty, so the remaining thing got destroyed again and again
Since toRemove is only a helplist, you need to reset it with clear as shown
but there are better ways to do it without using
toRemove
at allAlright :) I know I found out this way and I kinda like it so as long as it works properly, I don't mind ^^
this is without
toRemove
Okay thank you :) i'll check than soon ;)
Is there a way to speed up or slow down your animation without touching the framerate ? :)
Boy, have you even read my code???
Look at line 141
How did I even skip the only one I wanted Oo I saw the bool and the frame count but I didn't see this one ^^ I'm use to see that much comments maybe :) Not more questions anyways, I'll change your class to get other things but I won't require any further help :) Thanks ;)