2d coordinate system

0 favourites
From the Asset Store
2d mushroom sprite 2d game character enmy sprite game art
  • Ok I am going completely nuts here. Construct 2 is not easy coming from a C oriented language and trying to work without objective thinking. For 2 days I have searched and tried to figure out how to make something as simple as 2 planets that have either and x,y and a name or id of some sort.

    In C we would implement a class

    class planets

    {

    planets planet;

    planet.name={"Planet1","Planet2"};

    planet.x={235,14};

    planet.y={45,398};

    };

    etc, etc. You could call these and for ex. print a sprite at planet.x and planet.y locations etc.

    I want these to be saved values. I was thinking of a array such as planet[name_char][x_int][y_int] but I can not figure out how to do this in Construct 2.

    1. How do I store values in a 3 dimensional array maybe?

    2. How would I go about puting a sprite on the screen with that arrays data?

    3. Can you do this with a 3 dimensional array or should you do it some other way?

    Like I said for 2 days this simple little stupid function and I cant figure it out. I much have read through 200 forum posts or more and 40 tutorials!

    Please help!

  • What's wrong with instance variables?

    PlanetSprite

    • myID: Number
    • myX: Number
    • myY: Number
    • myName: Text

    edit: added the ID. You could store the planet ID's in a 2D array mapping to your coord system. That might remove the need to store x,y in each object. But I wouldn't go the 3D array route, it doesn't seem necessary.

    sorry if this is too simplistic and I have misunderstood your needs.

  • It sounds like your over-complicating it a bit? It is a change coming from something like C or Java to Construct. A lot of the concepts can be modeled the same, but the way of thinking of attacking a problem has be a bit different.

    For instance, to attach an array to a sprite you could create a container containing the sprite and the array. Everytime the sprite is created, the array will be created with specific values to that set as well.

    As Codah suggested, you could add instance variables to the sprite.

    More importantly though,the sprites hold their own values that don't need to be created for these. Like, if you want to reference the X coordinate of planet, in any of the condition or action statements, you would just use planet.X and likewise planet.Y. If you want to create a new object, use the system condition "create object" with whichever X and Y coordinate you want to use, or however you determine them.

  • Hrm, do you guys mean local instances variables? I was thinking down the line might have issues trying to call local instance variables. If you have other objects traversing my little 2D universe if you dont have a "planet" visiable you can not collide or discover that object unless you add a lot of invisible collision objects.

    It all takes time moving from a programming language to a visual design, but it can get Soooooooo frustrating haha.

    Thx for the info, will look into the instance variables some more see if I cant figure out how to access them globally etc.

  • Cybergod

    Instance variables although unique to each instance of the same object can be accessed and passed to global variables & to other objects.

    I threw together a little example of setting global variables to the instance variables of a clicked planet. Planets are invisible until the mouse is a certain distance away.

    Hope it helps with instance variables.

    [attachment=0:19wbzezk][/attachment:19wbzezk]

  • Minor

    Hrm, pretty much what Im looking for, but, where did you put the individual planet info/data???? lol.

  • In the layout editor.

    Click any of the planets in the layout editor and on left you'll see the "Instance Variable" panel you can edit them there on a per planet basis. You could of course add them with events especially if you wanted to randomise the values.

    Hope that helps.

  • Minor

    Ahh omg I thought you had to set it manually in the eventsheet

    with the

    "Set PLX to random(500)"

    "Set PLName "Planet1""

    and so forth in the Add Action lol.

    Im only about 2 weeks into Construct 2 but yah sometimes it can get really frustrating trying to convert C++ thinking into Visual Thinking hehe.

    Thx alot mate, this helps ALOT!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Yep, I've been struggling with the same kind of issues. You really have to approach these kind of things quite differently.

  • Minor

    You know what, this really wont work either.

    I have been playing around with it and because you have local instances of the x,y you can not randomize the locations out. For example

    On Start Of Layout Create Object Planet at X=(random(500), Y=random(300)

    This above capx you where kind enough to through together for me will only allow you to pre-place the planets and not randomize out tyhe x & y coordinates.

    Thx for the try though, still looking at it to figure it out and will post here if and when I do.

  • Try using the On Create trigger of the planet object to set your values.

  • Well it really has to be predefined. The values should be like following,

    <PlanetName> This is predefined (Char Variable)

    <PlanetMass> This is Predefined (Long Variable)

    <PlanetX> This can be random(x) on creation of object (Integer)

    <PlanetY> This can be random(x) on creation of object (Integer)

    So basically a couple things need to be predefined and some items can be random on creation or randomly predefined.

    Hope this explains it a little better and I really appreciate the help. I understand it can be hard to understand what someone is asking just through writing.

  • Without a definite idea of what you are trying to achieve all we can do is just keep trying to gently poke you in the right direction.

    For example if you have a set number of planets (i.e always 10 & always the same planets) you can fill in all the instance variables in the layout editor. Then you could randomize the X&Y at the start of layout.

    [attachment=0:1qzz5gir][/attachment:1qzz5gir]

    In the example the planets can overlap but it's just to show you that you could randomize X&Y even with planets predefined in the layout editor.

    If you're going to have random numbers of planets (possibly moons) then there are multiple ways of storing their data and populating the random planets at runtime. You could randomize them completely or randomly choose a planet from a list of predefined planets.

    If that's where you're heading then just post here and I'm sure you'll find the help you need.

  • I think you're complicating things. I also come from an OO background. C2 does have some OO concepts. Consider the following:

    Object Types:

    An Object Type is kind of like a class, with class variables (called instance variables in C2).

    Setting 'instance variable' values on an Object Type (via the properties in the editor or programatically) sets them for new, spawned Object instances.

    You cannot have an Object Type 'inherit' another.

    Objects:

    An Object is an 'instance' of an Object Type.

    Setting 'instance variable' values on an Object instance (via the properties in the editor or programatically) sets them for just that Object instance, overriding the Object Type.

    Families:

    A Family is kind of like an abstract class, with class variables (called Family instance variables).

    Object Types can 'inherit' from these by placing the Object Type into a Family (or more than one Family, for 'multiple inheritance').

    You cannot have an Family 'inherit' another (cannot place a Family in a Family). So 'inheritance' is only one level deep.

    Setting 'Family instance variable' values on an Object (via the properties in the editor or programatically) sets them for just that Object instance, overriding the Family.

    Note: Effects and Behaviours work similarly to Instance variables on the above.

    Apologies for any mistakes in the above, which I'm sure I'll be corrected on

  • Ya I see what you are saying but to do it that way is possibly not doable in larger universe.

    Say for example you have a playfield, x=0, y=y to x=5000, y=5000. A fairly large playfield.

    Now you like to have 200 planets. You want pre-defined names, so you need a list of 200 names.

    You want a random placement of the planets through plx=(1+random(5000)) and ply=1+random(5000)

    Now if we do like the above example you would have to make a sprite object, 200 of them and

    fit them in on the screen just so you can pre-define some values to them.

    This is where C++ came in handy with its classes, vectors and arrays.

    Or what do you think?

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)