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 / Pesky Arrays

Author
Message
sgmedia2
11
Years of Service
User Offline
Joined: 30th Oct 2012
Location:
Posted: 30th Oct 2012 23:12
Hi all, been having trouble assigning ID's to my array sprites. If I have an array, dim myvariable[30]as mytype for example, how would i load a sprite with ID[x] using a for loop only if getpointerpressed() = 1. And now it would look like this.

dim myvariable [30] as mytype
for x = 1 to 30
myvariable[x].ID = x
next x

if getpointerpressed () = 1
createsprite(x,loadimage("image.png))
set physics and stuff
endif

This workd fine for loading multiple sprites to the screen at one time, but i need to send a single sprite at a time to the screen with a new ID each time I left click.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 31st Oct 2012 16:16
How is the value of x being set in this statement?


Your posted code does not show you using your array in the createsprite step.

Also, if you are going to be using the same image more than once, you should load it once outside the main loop and just use the image id in your sprite creation. Otherwise each sprite creation takes up more space than necessary because you keep loading the image again.

Cheers,
Ancient Lady
AGK Community Tester
Marl
12
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 31st Oct 2012 20:44
@sgmedia2, Putting the array index into an array field really doesn't get you anything unless the value will be changing later.

It might suit you better to let the system generate the sprite ID's and then you simply store them in the array.

The way I do it ( adapted to your code), would be;

The initial loop simply sets the initial value of each ID to zero - always good practice to initialise fields of a user defined type before use.

The sprite is created with the other format of the command - where the system gives you the sprite ID - the resulting value is stored in the array at position x in the ID field.

Later when you come to use it, you can check if it is zero for safety and then use the commands directly on the array element, with something like;

I am presuming here that x will represent a reference to an object of some kind, and myvariable[x].ID will hold the ID of the sprites for the objects in the array.
sgmedia2
11
Years of Service
User Offline
Joined: 30th Oct 2012
Location:
Posted: 9th Jan 2013 16:38
Hi again everybody. Got great advice on my last post yet I have run aground again. Does anyone know how to trick AppGameKit into using negative numbers and positive numbers within a random.

example: setspriteposition(x,0,random(-10,10))
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 9th Jan 2013 17:51
x = RandomSign(Random(0,10))

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 9th Jan 2013 19:29 Edited at: 9th Jan 2013 22:01
Or if you want less functions:



Would work just the same as previous example.

Another example might be:
Let's say you want to have a random number between -5 and 5.
You get it this way:



Follow me on twitter! @MotionStruct
Motion Struct blog
Marl
12
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 10th Jan 2013 19:35 Edited at: 10th Jan 2013 19:37
Quote: "myNumber = Random(0,10) - 5"

This way, every time - unless you want floats, in which case something like;


Which is confusing using 10 as a limit, so with 9 it would be;

which generates a float between 0.000 and 9.000

NB: "10^(DEC_PLACES)" should be picked up by the compiler and replaced by a constant so the powers are not being evaluated at run time

Login to post a reply

Server time is: 2024-05-03 21:20:43
Your offset time is: 2024-05-03 21:20:43