Enemy Question

i watched the tutorial for strike and interact and my problem is that the enemys are duplicate unlimited

is this code right?

//init initEnemies = function() enemies = [] for i = 1 to 3 spawnEnemy() end end

spawnEnemy = function() local enemy = object active = true size = 40 end local placed = false while not placed enemy.x = randRange(-175, 175) enemy.y = randRange(-75, 45) if distance(enemy.x ,enemy.y , player.x,edd ddrdwa player.y) > 30 then placed = true end end enemies.push(enemy) end

updateEnemy = function() temp = [] for enemy in enemies if distance(enemy.x,enemy.y,player.sword_x,player.sword_y) < 15 then enemy.active = false end if enemy.active then temp.push(enemy) end end enemies = temp end

drawEnemies = function() for enemy in enemies screen.drawSprite("cookie",enemy.x,enemy.y,50 ,50 ) end end //player screen.setDrawScale(player.facing, 1) screen.drawSprite("maincharakter", player.x, player.y, player.width, player.height)

 if map == "flurvorinformatik" then


if player.attack_timer > 0 then screen.setDrawAnchor(0.5, 0) screen.drawSprite("stift",player.sword_x,player.sword_y, 32, 12 ) screen.setDrawAnchor(0,0) end screen.setDrawScale(1,1)

//drawfunction draw = function() drawStart() drawPlayer() drawDoor() drawLife() playeranim()

if distance(player.x, player.y, door.x, door.y) < 25 then screen.clear map = "flurvorinformatik" drawFlur()

if map == "flurvorinformatik" then drawFlur()

drawPlayer() spawnEnemy() drawEnemies() end

if  game_paused then


end end

First this is unredable...
Second sorry but i cannot decript this next time put link to the project instead (you can make it public but not listed)
Third there is an really easier way but it's more advanced:

Here you will need these functions

List.draw = function()
  for t in this

List.update = function()
  for t in this

// removes objects from a given list that have a property "destroy = true"
List.destroy = function()
  for t in this
    if t.destroy then 

you make enemy as follows:

enemy = class
  constructor = function(//you can pass arguments here)
  //works as int
    this.x = (-screen.width/2)-16 or (screen.width/2)+16
    this.y = random.intRange(-screen.height/2,screen.height/2
    this.xv = 0
    this.yv = 0
    this.angle = 0
    this.velocity = 1
    this.destroy = 0
    this.sprite = 'enemy'
    this.width = 16
    this.height = 16
  update = function()
    //here you put updates
  draw = function()

you make list of enemies you can push them to list by yourself or by spawner

//if you push enemies
enemies.push(new enemy())
//if u want spawner
enemy_spawner = every 2 seconds do enemies.push(new enemy()) end
//in the update function you need to have

I know this isnt answer that you were looking for but I hope thet it will help.

UPDATE : i have fixed that there duplicating but there are duplicating anywhere but if i stop my project there are only 3 enemies that the number i want but if i start there glichting and spawns new enemies anywhere

Can you share project so we can take a look at code?
I meen at this point it's just guessing game of what is wrong with the code.
Go to publish Make public and post the link. Nobody can help you if we cannot look at the code.

ok i made my project public name = Lost in school if you star t the project you can go into the door and then you see the bug https://microstudio.dev/i/Sammy4141/lostintheschool23/

Where to even begin with this project
First It's not the bug everything works as you coded it.
Second your code is a mess I know you are a kid and just figuring things out but you need better code management.

Enemies are not duplicating you have exactly one enemy but that enemy changes x and y
because variable placed is always false so that means that you have infinite loop in your code and that is well bad.
Why you have QuickEngine but not using it thats mistery for me.

The further that I go into this project the more errors I find why do you call initEnemy and drawEnemy on every keypress
I fixed enemy but then game started lagging because of it..

At this point I'm really sad to say this but it will be beter even easier to start this project over. I'm sorry...

If u need any help, advice even online meet to talk and do this together the right way. I will make my time..
I see that you are trying and willing to learn so I really do want to help..

ok ): i start the project new but can you give me tips for using CuickEngine and other codes

im editing my game to 2d

Sure first you need state manager for handling different maps and scenes.
I see your project as some form of RPG so good statte manager goes a long way...

start by cloning State manager code by @JimBOO7 or copy paste to your project
that will help you a lot with maps...
Also player code must NOT have any other code but for player and interactions.

@Lucifer can your join my project for help

yeah I joined You didn'tt quite get concept of states point is you can have them as many as you need.
I will see later what I can do currently I'm wrestling some bugs of my own code.


@lucifer can you help me now

Sorry, mate I thought you don't want to work with me, thought you were skeptical... How were I wrong, sorry again for whatever reason I've got this in spam folder... Now that I whitelisted you... you can contact me on the same mail...

Post a reply



Validate your e-mail address to participate in the community