Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
base class - Gideros Forum

base class

I would need your help, please.

I don't know how to create a base class for my enemies.

For example I have one enemy class:
Enemy01 = Core.class(Sprite)
 
function Enemy01:init(xname, xposx, xposy)
	-- settings
	self.name = xname
	self.x = xposx
	self.y = xposy
	self.vx = 0
	self.vy = 0
	self.flip = 1
	self.isonfloor = false
	self.isattacking = false
	self.lives = 3
	self.accel = 500
	self.friction = 150
	self.maxspeed = 150
	self.jumpspeed = GRAVITY / 2
end
 
function Enemy01:createAnim(xanimname, xstart, xfinish, xanimslist)
	self.anims[xanimname] = {}
	for i = xstart, xfinish do
		self.anims[xanimname][#self.anims[xanimname] + 1] = xanimslist[i]
	end
end

I have different enemies which would have different parameters value.
How can I make a base class which my enemies would inherit (variables and functions).

I have searched on the forum but I don't know how to handle this.

Thank you in advance for your time.

Comments

  • Apollo14Apollo14 Member
    edited August 13 Accepted Answer
    EnemyAdvanced = Core.class(Enemy01)
     
    function EnemyAdvanced:init()
     
    end
    :)

    Likes: hgy29, MoKaLux

    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
    +1 -1 (+2 / -0 ) Share on Facebook
  • hgy29hgy29 Maintainer
    Accepted Answer
    Note that when using Core.class() to create a sub-class, the arguments sent to your new/init function will be used to call the superclass init too, which can have undesired effects. To handle this, I use my own Core.class call:
    Core.classArgs=function (p,a)
    	local c=Core.class(p)
    	c.__new = function(...)
    		local b = getmetatable(c)
    		local s1 = b.__new(a(...))
    		setmetatable(s1, c)
    		local init = rawget(c, "init")
    		if type(init) == "function" then
    			init(s1, ...)
    		end
    		return s1
    	end
    	return c
    end
    It takes a second argument, which is a function that will return the arguments intended for the superclass based on the new class constructor arguments. Example:
    -- Define a new class to make a 50x50 pixel. Argument is the plain color	
    Pixel50=Core.classArgs(Pixel,function(color) return color,1,50,50 end)
     
    --Use it
    stage:addChild(Pixel50.new(0xFF0000)) -- Create a 50x50 red pixel
    I was thinking about enhancing gideros Core.class to do the same (if second arg is present), what do you think ?
    +1 -1 (+3 / -0 ) Share on Facebook
  • thank you guys for the answers.

    @Apollo14 I knew this bit Enemy02 = Core.class(Enemy) but then what do I put in the init()? only the values that are going to change? For example:
    Enemy02 = Core.class(Enemy)
    function Enemy02:init()
       self.vx = 30
       self:createAnim("walk", 1, 6, myanims_list)
    end
    Ok, I should experiment more. Right now I do it the "easy" way but I feel like my code isn't nice.

    @hgy29 I have read on the forum about Core.class and how it is implemented by @atilim . That is a little bit too advanced for me :/ so I cannot tell what should be done.
  • antixantix Member
    Accepted Answer
    With my games I haven't had to create subclasses, I just have a class for enemy which gets passed options that are overwritten on init() to make it different to other enemies. In fact all of my GUI objects use the same method.
    Enemy = Core.class(Bitmap)
     
    function Enemy:init(texture, options)
      -- default options
      self.position         = {x = 0, y = 0},
      self.velocity         = {x = 0, y = 0},
      self.alive            = true,
      self.type            = 'MUTANT',
      self.canBeClicked     = true
     
      -- copy any supplied options
    	if options then
    		for key, value in pairs(options) do
    			self[key]= value
    		end
    	end
     
      -- etc, etc
    end
    Not the greatest code in the world maybe, but it works.

    Likes: MoKaLux

    +1 -1 (+1 / -0 ) Share on Facebook
  • @antix excellent, I am ending up doing something similar, maybe not the best way but it's easier for me! Thank you for sharing.

    Likes: antix

    +1 -1 (+1 / -0 ) Share on Facebook
Sign In or Register to comment.