help with class function
I don't quite understand class and constructor functions. Could someone explain them to me with examples, please? Thank you.
I don't quite understand class and constructor functions. Could someone explain them to me with examples, please? Thank you.
class is pretty similar to objects if you're familiar, but i find generally more useful. constructor is just a function to describe the object.
enemy = class
constructor = function()
x = 0
y = 0
width = 32
height = 32
end
end
you can also put functions in classes;
update = function()
enemy.moveLeft()
end
enemy = class
constructor = function()
this.x = 0
this.y = 0
this.width = 32
this.height = 32
this.speed = 3
end
moveLeft = function()
this.x -= this.speed
end
moveRight = function()
this.x += this.speed
end
end
and last but not least, you can extend them
boss = class extends enemy
constructor = function()
super()
this.width = 48
this.speed = 1.2
end
end
super() makes sure everything from the class it extends from is carried over, and functions are automatically carried over or you could add the function and adjust it.
So, does that mean all objects of the same class will have the same code? That's great, it avoids repetition. If I want to create enemies that appear randomly, I can use the class function, but how do I do that?
And also, is it possible to make multiple objects solid with this class (like a function that says all objects named "wall" are solid)? Thank you very much.
all objects made using "name = new className" will start off identically. you can make stuff spawn randomly like so:
init = function()
enemies = []
for i = 1 to 15
local slime = new enemy
slime.randomize()
enemies.push(slime)
end
end
enemy = class
constructor = function()
this.x = 0
this.y = 0
this.width = 32
this.height = 32
this.speed = 3
end
moveLeft = function()
this.x -= this.speed
end
moveRight = function()
this.x += this.speed
end
randomize = function()
this.x = random.nextInt(200)-100
this.y = random.nextInt(200)-100
end
end
this would theoretically spawn 15 slimes up to 100 pixels away from the center. you could adjust the randomize function as you see fit.
so while I don't think you could do the wall thing with just the class name (maybe you can, I'm not sure), you could give the original class a variable like "solid" or something and make it true so that all extended classes also have that variable and you can just check which ones have "solid" marked true.
No, I was asking if there was a function that allowed sprite detection on the map and, in theory, created code with it.
i'm not sure what you mean, if you're talking about getting what sprite a tile is on a map, that is a completely separate thing that you could use the collisions or quick engine libs for
I'm creating an RPG game and I was wondering how I can handle map collisions (like tiles and objects placed on the map). I want code that checks if the player is standing on an object placed on the map (for example, if an object icon is placed on the map, it should permanently prevent the player from moving). I want someone to explain tilemap collisions to me. Thank you for what you've already taught me Actes.