• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

Way to implement 2 data types in a parameter for a function

john2367

New member
Joined
Feb 19, 2019
Messages
10
Is there a way to make a parameter data type in a function optional? I'm trying to create a code that detects collision and automatically removes it from the list that they belong to. However, the objects that I will use collision on have different classes.

Cerberus:
Function LaserColl:Void(laserList:List<Laser>,againstList:List<Alien> Or <Shield1> Or <Shield2>)
    For Local laser:=Eachin laserList
        For Local against:=Eachin againstList    'removes laser and alien if collides
            If Collision( laser.x, laser.y, 1,7,against.x,against.y,15,15) Then
                laserList.Remove laser
                currentScore += against.value
                against.lives-=1
                If against.lives < 1 Then                            
                    againstList.Remove against
                Endif
            Endif
        Next          
    Next
End

I want to make the code modular so that it suits different classes(that are stored in a list)
 
So your againstlist contains all objects but only a subset has the Collision function?

Or is the problem to store your alien/shield/whatever in that againstlist?
 
Quick and dirty solution: add another parameter that indicates the class being passed then use a select statement to execute the required code.

Better still, you might be able to reduce the amount of code by creating a temporary list containing a copy of the list in the parameter.

There are probably more elegant solutions :D
 
So your againstlist contains all objects but only a subset has the Collision function?

Or is the problem to store your alien/shield/whatever in that againstlist?
Yes, I was hoping to store alien/shield/whatever in the againstlist
 
Quick and dirty solution: add another parameter that indicates the class being passed then use a select statement to execute the required code.

Better still, you might be able to reduce the amount of code by creating a temporary list containing a copy of the list in the parameter.

There are probably more elegant solutions :D
That is actually quite a good idea
 
Yes, I was hoping to store alien/shield/whatever in the againstlist
I am sure that these objects share some methods and fields. And that they are extended from a base class.

So make the againstlist as one of the base class. That should do the trick.

Cerberus:
Strict

Import mojo


Class baseClass
    Field x:Int = 0
    Field y:Int = 0
    Field xf:Int = 0
    Field yf:Int = 0
    Method update:Void() Abstract
End

Class object1 Extends baseClass
    Method New(_xp:Int,_yp:Int,_xf:Int, _yf:Int)
        x=_xp
        y=_yp
        xf=_xf
        yf=_yf
    End
    Method update:Void()
        x += xf
        y += yf
        If x < 0 Then x+=640
        If x > 640 Then x-=640
        If y <0 Then y+=480
        If y >480 Then y-=480
    End
End

Class object2 Extends baseClass
    Method New(_xf:Int)
        xf=_xf
        x= 320
        y= 240
    End
    Method update:Void()
        x += xf
        If x < 0 Then x+=640
        If x > 640 Then x-=640
    End
End

'-----------------------------------------------------------------
Class myClass Extends App
    Field objList:= New List<baseClass>
    '-----------------------------------------------------------------
    Method OnCreate:Int()
        SetUpdateRate 60 
        objList.AddLast(New object1(320,240,1,1))
        objList.AddLast(New object2(-1))
        Return 0
    End
    '-----------------------------------------------------------------
    Method OnUpdate:Int()
        For Local obj:= Eachin objList
            obj.update()
        Next
        Return 0
    End
    '-----------------------------------------------------------------
    Method OnRender:Int()
        Cls
        For Local obj:= Eachin objList
            DrawRect(obj.x, obj.y, 20,20)
        Next
        Return 0
    End
End

'-----------------------------------------------------------------
Function Main:Int()
    New myClass
    Return 0
End
 
Back
Top Bottom