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 / Multiple sprite clones doing the same behaviour help

Author
Message
Midget Blaster
AGK Developer
8
Years of Service
User Offline
Joined: 7th May 2016
Location:
Posted: 17th May 2016 19:56
Currently I tried :

clones = CloneSprite (enemy)

DIM clones[5]
clones[1] = cloneaction()
clones[2] = cloneaction()
clones[3] = cloneaction()
clones[4] = cloneaction()
clones[5] = cloneaction()

function cloneAction()
if GetSpriteCollision(player,enemy)
setspriteposition(player,200,550)
endif
endfunction

function clonePlacement()
setspriteposition(clone[1],100,120)
setspriteposition(clone[2],256,200)
setspriteposition(clone[3],350,400)
setspriteposition(clone[4],20,900)
endfunction

How can I easily make a lot of enemy clones, that do the collision function command and that I can place them separately?
Thanks for your help
Jack
19
Years of Service
User Offline
Joined: 4th Oct 2004
Location: [Germany]
Posted: 18th May 2016 14:16 Edited at: 18th May 2016 15:10
Hello Midget_BlasterxXx420BLAZEWEED,

I do have to admit, that you have the longest name I saw here for a while. Even the board can't handle it, but maybe we can.
Your problem is the basic structure, it won't work like you did, but here is a good solution:

First, you have to create an array holding all information of the enemies. If you use a type, you can add additional information like amount of armor or life.



Alright! Most of the stuff is already done! Now we want to create a function, that can add an enemy.



Alright, now we can add simple enemies. If you want to update them properly, you will need an update function inside your main loop:


That's all!


The basic structure in a code should look like this:


I have not tested the code, but the concept is correct.

[/url]
Midget Blaster
AGK Developer
8
Years of Service
User Offline
Joined: 7th May 2016
Location:
Posted: 18th May 2016 16:40
Great examples!

So from your code, I wrote



The level1() function works just how I wanted, because I can place sprites separately in my level.
However, the problem being that the only thing the enemy needs to do is

if GetSpriteCollision(enemy1,player)
setspriteposition(player,200,550)
StopMusic()
PlaySound(dieS)
PlayMusic(1,1)
endif

and when I have placed 3 of them in my level, only one of them actually followed that collision statement. So what I tried was

for i = 1 to enemy1.length
if getSpriteCollision(player, enemy1[i]) = 1
setspriteposition(player,200,550)
StopMusic()
PlaySound(dieS)
PlayMusic(1,1)
endif
next i

but that didn't work xd. Where did i go wrong? Thanks lads

CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 18th May 2016 17:16
Quote: "for i = 1 to enemy1.length"
enemy1 is the image id for enemy.png, not the array.
V2 T1 (Mostly)
Phone Tap!
Uzmadesign
Midget Blaster
AGK Developer
8
Years of Service
User Offline
Joined: 7th May 2016
Location:
Posted: 18th May 2016 22:03
But even if I change that, I cannot use a sprite and an array in a getSpriteCollision command. Any other ideas?
Jack
19
Years of Service
User Offline
Joined: 4th Oct 2004
Location: [Germany]
Posted: 19th May 2016 07:29 Edited at: 19th May 2016 07:35
for i = 0 to enemy1.length // 0 is the first entry in the array, not 1
if getSpriteCollision(player, Enemy[i].spr) = 1 // this will perform the collision with the player sprite and the sprite from the array
setspriteposition(player,200,550)
StopMusic()
PlaySound(dieS)
PlayMusic(1,1)
endif
next i





getSpriteCollision(player, Enemy[i].spr) = 1
Enemy[i].spr is the variable of the enemy sprite, saved inside the array


As I see, you need to learn to handle arrays.
I promise, If you learn the following stuff, and do some array fun experiments first, you will win fast!

https://www.appgamekit.com/documentation/language/5_dim.htm
https://www.appgamekit.com/documentation/guides/12_array_changes.htm

Good experiments with arrays:
- Use Types in arrays
- Use Types in Types
- create an array for your settings! Like sound volume or resolution information
- Display each information of the array
- Modify an array in length
- Delete an entry in the array
- Try dynamic array lists, once an enemy get killed, it should be removed from the array (does the update code still work, when you implement it? When not, why? Try to answer it with the links above)
- Shift array function (Can you manage to create one?)


If you get pro with arrays, you get pro in game-developing.

[/url]
Midget Blaster
AGK Developer
8
Years of Service
User Offline
Joined: 7th May 2016
Location:
Posted: 19th May 2016 19:05
I started from scratch and I made it work! thanks so much for your help Jack.

This it what I came up with



And it works like a charm! Only problem now is when I"m making the game I need to place these enemies about a couple hundred times per level, so they all need their separate placing.
Thanks again lads
Midget Blaster
AGK Developer
8
Years of Service
User Offline
Joined: 7th May 2016
Location:
Posted: 19th May 2016 22:03
I've ran into a bit of a problem.


Since myEnemies [i] = CreateSprite(enemy) is in this function, and this function is in a loop, what I think is happening is that the sprite is being created 60 times a second (fps) and it it absolutely crushing my fps, especially on my phone. Any optimization I could do? Whenever I declare myEnemies [i] = CreateSprite(enemy) outside of the function it always breaks my game. Anything you guys would do better drop it here.

As for linking me to https://www.appgamekit.com/documentation/guides/12_array_changes.htm , It is an excellent learning source, but it will take me half a week to fully understand it :p
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 20th May 2016 08:37
In that case I suggest you invest half a week understanding it.
It will take a lot longer than that if you continue going back and forth asking people to fix your broken code when you don't understand what it is that you're doing.
AGK V2 user - Tier 1 (mostly)
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 20th May 2016 09:20
Take a look at this..

In the first loop, when I = 0, you creat one, and only one sprite, assigned to index 0. Then you try and position sprite 0 and 1 and 2 etc, which do not exist. You cannot change the position of a sprite that doesn't exist. You also do not close your for loop with a next command.



As much as I loathe providing complete code snippits to people learning the basics, here is some working code for you.

Login to post a reply

Server time is: 2024-09-29 15:14:25
Your offset time is: 2024-09-29 15:14:25