How to dynamic load the image of sprite?

Discussion and feedback on Construct 2

Post » Tue Nov 08, 2016 2:26 am

I use construct 2 to create my first Html5 game.And I have a request that random to load image.For example,I have 5 images in my image folder,but every time user enter the game ,it just need to load one.
So I create a sprite object first and I want when the page loading the sprite can random to set a image by global variable.How can I do?Or any methods?Thank you.
B
9
S
2
Posts: 13
Reputation: 551

Post » Tue Nov 08, 2016 6:36 am

yes, just like you did,
first step, create a new sprite object load images from your file and named.
secondly, create a global variable, on the start of layout, set this variable to random(sprite.animationFrameCount+1)
last, set sprite frame to variable.
B
72
S
25
G
5
Posts: 90
Reputation: 7,277

Post » Tue Nov 08, 2016 7:52 am

totoyan wrote:yes, just like you did,
first step, create a new sprite object load images from your file and named.
secondly, create a global variable, on the start of layout, set this variable to random(sprite.animationFrameCount+1)
last, set sprite frame to variable.

Thank you so much.One question,I want to know how to set sprite frame to variable?Do you mean use the sprite event "load image from url"?Or any other way?
B
9
S
2
Posts: 13
Reputation: 551

Post » Tue Nov 08, 2016 5:47 pm

KKdenis wrote:I use construct 2 to create my first Html5 game.And I have a request that random to load image.For example,I have 5 images in my image folder,but every time user enter the game ,it just need to load one.
So I create a sprite object first and I want when the page loading the sprite can random to set a image by global variable.How can I do?Or any methods?Thank you.



if you have the images inside a sprite, use the following


on start of layout - sprite.setframe=round(random(self.animationframecount))


see if that helps. keep in mind, using the animation frame count and a random of that total number, might be that some times, on start of layout you might get the same frame to show, since has no actual order.
B
78
S
23
G
69
Posts: 1,353
Reputation: 44,005

Post » Mon Nov 14, 2016 1:29 am

KKdenis wrote:
totoyan wrote:yes, just like you did,
first step, create a new sprite object load images from your file and named.
secondly, create a global variable, on the start of layout, set this variable to random(sprite.animationFrameCount+1)
last, set sprite frame to variable.

Thank you so much.One question,I want to know how to set sprite frame to variable?Do you mean use the sprite event "load image from url"?Or any other way?



... i mean when you create a sprite object, import frames from your file and choose all your images, then set the event that "on the start of layout -- action: set sprite frame to int(random(sprite.framecount+1))"
B
72
S
25
G
5
Posts: 90
Reputation: 7,277

Post » Mon Nov 14, 2016 2:16 am

gamecorpstudio wrote:
KKdenis wrote:I use construct 2 to create my first Html5 game.And I have a request that random to load image.For example,I have 5 images in my image folder,but every time user enter the game ,it just need to load one.
So I create a sprite object first and I want when the page loading the sprite can random to set a image by global variable.How can I do?Or any methods?Thank you.



if you have the images inside a sprite, use the following


on start of layout - sprite.setframe=round(random(self.animationframecount))


see if that helps. keep in mind, using the animation frame count and a random of that total number, might be that some times, on start of layout you might get the same frame to show, since has no actual order.

Thank u ,it is fine.
B
9
S
2
Posts: 13
Reputation: 551

Post » Mon Nov 14, 2016 2:17 am

totoyan wrote:
KKdenis wrote:
totoyan wrote:yes, just like you did,
first step, create a new sprite object load images from your file and named.
secondly, create a global variable, on the start of layout, set this variable to random(sprite.animationFrameCount+1)
last, set sprite frame to variable.

Thank you so much.One question,I want to know how to set sprite frame to variable?Do you mean use the sprite event "load image from url"?Or any other way?



... i mean when you create a sprite object, import frames from your file and choose all your images, then set the event that "on the start of layout -- action: set sprite frame to int(random(sprite.framecount+1))"

Thank u so much.It is work
B
9
S
2
Posts: 13
Reputation: 551


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 10 guests