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.

Newcomers AppGameKit Corner / [QUESTION] About creation, rotation and color

Author
Message
Kozax
9
Years of Service
User Offline
Joined: 28th Jul 2015
Location: London, UK
Posted: 1st Aug 2015 21:47
Hello

I have another problem I would like help with.
I need every second to create a specific sprite and each time to create it at a random position, change its color and keep rotating the sprites indefinitely so I did:



This way the colors don\'t change and the sprites only rotate for one second each.
Could you point me to the problem that is causing this?

Thank You
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 2nd Aug 2015 02:37
Every time you clone the sprite, you are overwriting your reference to it in Circle1Clone. The sprite still exists, but you have no way of referring to it in your prgram.

You need an array:

Dim circleClone[10] as integer.

This will give you 10 elements that you can save your sprite numbers in. (It's actually 11, but that's for another day!)

Look up the array help if you haven't used them before. They are on eof the cornerstones of dynamic programming.

Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Kozax
9
Years of Service
User Offline
Joined: 28th Jul 2015
Location: London, UK
Posted: 2nd Aug 2015 12:51
Hello and thank you for your quick response

The Dim and Arrays documentation didn't include an example with infinite number of spawns

Could you please direct me to a tutorial or a guide?
I don't need to have an exact amount of copies as the sprite will be create infinite times.

Thanks
Kozax
9
Years of Service
User Offline
Joined: 28th Jul 2015
Location: London, UK
Posted: 2nd Aug 2015 13:00
Addition to last post:

This time I did:



But still, the way I do this only references the last sprite created also the color doesn't change.
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 2nd Aug 2015 14:18
Here you go, your code amended with lots of comments...



Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Kozax
9
Years of Service
User Offline
Joined: 28th Jul 2015
Location: London, UK
Posted: 2nd Aug 2015 14:44
Thank you for writing the code but it wasn't necessary.
I really appreciate it.

Everything works fine but only the color problem persists.
I noticed that the sprites created only keep the alpha value from the SetSpriteColor command and for some reason it doesn't change the color of the sprite. If I use the cube instead of the image the color is changing properly.

- I copied and pasted your code exactly as you wrote it.
- I tried using the mole from the example project and the color is changing properly.
- I use a black circle in png format for the image, what do you thing this is causing it? I created the circle in photoshop.(I uploaded the image)

Cheers

Attachments

Login to view attachments
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 2nd Aug 2015 14:47
The colour commands affect white areas of the image.
A pure white image will inherit the full colour you apply to it.
A 50% grey image (128,128,128) will inherit half the colour you apply to it.

Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Kozax
9
Years of Service
User Offline
Joined: 28th Jul 2015
Location: London, UK
Posted: 2nd Aug 2015 14:53
Thank you for clarifying that, it works!
Kozax
9
Years of Service
User Offline
Joined: 28th Jul 2015
Location: London, UK
Posted: 22nd Aug 2015 17:53
Hello again,

Instead of creating a new post I continued this one because I'm using the same code and it is somehow connected with my previous question.

I used the code above and it works perfectly.
I tried to add more stuff to the original code. For example I wanted all of the circles to destroy at some point.

So I did:


But it doesn't work, nothing is getting deleted.

Also I have one other question, I don't want to spawn only one kind of circle but more. Should I use another sprite or the same sprite and change the animation and size?

Thank you!
Uncle Martin
18
Years of Service
User Offline
Joined: 9th Jul 2006
Location: Tampa, Florida, USA
Posted: 23rd Aug 2015 01:53
In your "if" statement, I think you accidentally used
GetSpriteY
when you meant to use
GetSpriteAngle

As far as the second question, I would say that it totally depends on what you are trying to do. Experiment & see what works best for your situation.

Enjoy.

Code every line like it might be your last...
Someday it will be.
Kozax
9
Years of Service
User Offline
Joined: 28th Jul 2015
Location: London, UK
Posted: 23rd Aug 2015 01:59
Actually I need it to be deleted as soon as it falls after 340. But it doesn't work for some reason.

I experimented with my previous question and creating more sprites worked better.

Also, I would like to know, is there any way to create a group of sprites(not for collisions), for just referring purposes. It's really annoying having to write the same things over again.

Thank you for your reply

Cheers
Kozax
9
Years of Service
User Offline
Joined: 28th Jul 2015
Location: London, UK
Posted: 24th Aug 2015 15:56
I am still struggling with the sprite deletion, it is a crucial part of my game and I would love any kind of response.
Currently, the code looks like this:



Everything works fine except the last part where I try to delete all the instances of the box when the game has ended and the Game Over menu is visible. The game wont delete the boxes after the GameMode has changed. Probably I am doing something wrong with the box referencing.
Any info could help.

Thank you for reading!
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 24th Aug 2015 16:13


You don't need to set Boxes[n] to zero, but it keeps it tidy.

Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Kozax
9
Years of Service
User Offline
Joined: 28th Jul 2015
Location: London, UK
Posted: 24th Aug 2015 17:05
Hey BatVink,

Thanks very very much for helping me! It worked

Before I checked your answer though, I noticed that by deleting the boxes with this line:



made the game to lose fps after a while(30-40 seconds of playing)
If I disable this line the game runs smooth(60fps) and the falling objects remain on the ground and don't affect the framerate.

I am sure that this is the only line that affects the fps.
Do you know why by deleting the objects, it decreases the framerate?

Cheers
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 24th Aug 2015 22:14
It is probably because you didn't delete the array elements.

If for example you created 100 boxes per second, then after 40 seconds you are having to check 240,000 elements a second (4000 boxes at 60 FPS).

Even without that many elements, you would be performing GetSpriteY() on a sprite that doesn't exist. This will generate an internal error, which is probably far more inefficient than checking a sprite that exists.

Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Kozax
9
Years of Service
User Offline
Joined: 28th Jul 2015
Location: London, UK
Posted: 24th Aug 2015 22:50
I can't express how grateful I am to you for helping me with this matter.
I thought that the array was causing a problem to the game making it slower.

So in this case what do you suggest me to do?
Should I use another command instead of GetSpriteY()?
Or just delete the array elements?
I tried removing the elements using Boxes[ThisBox].remove() but it gives me an error.

Again, thank you very much for your help...still learning
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 25th Aug 2015 00:10 Edited at: 25th Aug 2015 00:11
To prevent the array element remove error, you need to work in reverse...



As soon as you remove an element, the array length is reduced by one. But the "For" loop does not adjust it's original length. Eventually, you try to read an element that no longer exists because the length is shorter. For example you try to read element 10, but you deleted one so it is now only 9 elements long.

Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Kozax
9
Years of Service
User Offline
Joined: 28th Jul 2015
Location: London, UK
Posted: 25th Aug 2015 16:00
Hello,

I tried adding the code you gave me and it goes like this:



The problems are two.
1. In Boxes[n].remove(), it says unexpected token "." Array must be followed by an equals and an expression here. Its the same error I got when I tried the same command before.
2. The code to delete the boxes once the game is over deletes also the initial box that is created and when you press replay it doesn't spawn any boxes at all.
So I put on Start pressed(the button you press to start the game) to create the array again but it doesn't seem to work.



Thanks!
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 25th Aug 2015 16:31
I don't use remove, I got the syntax wrong...

Boxes.remove(n)

Quote: "The code to delete the boxes once the game is over deletes also the initial box that is created..."


Remove zero from the loop...

For n = Boxes.length to 1 step -1

Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Kozax
9
Years of Service
User Offline
Joined: 28th Jul 2015
Location: London, UK
Posted: 25th Aug 2015 17:45
Hello again!

The second part was fixed and works perfectly!

For the first question I modified the code to this:



When I run the game it says: Array index out of bounds, Index 0, Array Length:<empty> in main.agc at line 693

Line 693 contains this: SetSpriteVisible(Boxes[ThisBox], 1)
Its the first line that uses "Boxes[ThisBox]" in the main game loop in GameMode = 2

Cheers and again thank you!
Kozax
9
Years of Service
User Offline
Joined: 28th Jul 2015
Location: London, UK
Posted: 27th Aug 2015 19:37 Edited at: 28th Aug 2015 20:55
I figured this out, Thank you BatVink for all your help and patience!

Login to post a reply

Server time is: 2024-11-23 11:09:42
Your offset time is: 2024-11-23 11:09:42