Creating canvas through code

I am making a game with zombies in it. Basically what I want to do is be able to create the zombies (which I have on canvases) in the beginning of a wave. Is there a way to put a canvas on the screen through code instead of having to drag it on there? Is it even possible?

I think you need to rethink your setup. You should probably be using one canvas and then having that one canvas draw all of the zombies currently on screen instead of many canvases, one for each zombie. Also, you cannot programmatically create controls so adding canvases through code would not work unless you create a special container control to hold the canvas (which would probably get messy).

If you create a class to represent the zombies then at the start of the wave you can append new instances of the class to an array stored as a property in the window. Then in the canvas paint event, simply use a for loop and draw each zombie in the array.

Ya I was thinking about doing that but the problem is I already have all the code setup to work with the canvas moving so I would have to redo all of the code if I wanted to just draw them on the canvas. I’m also using many zombie pictures from sprite sheets to create walking and attacking zombies by changing out the backdrops of the canvas. But since there is no way to create a canvas it seems like I may have to just go back through and change the code

Like Jason suggested you should refactor your code to use one canvas. You will save yourself future “pain” if you do that now vs. later.

You can’t create a control, but you can duplicate it. Create a Canvas and move it offscreen. Call it something like CanvasTemplate and set it’s index to 0. Then:

Dim c as new CanvasTemplate

(I’m doing this from memory.)

However, I agree with the others about refactoring your code.

In the zombie class you could keep track of the pictures then have a “Drawme” method which returns the proper picture. I have a fairly simple animation class I could probably separate from a project that probably does a lot of what you need - it allows for simple image changing at a specific fps, storage of width, height, left, and top for drawing purposes and exporting and restoring instances using JSONItems (allowing saving to file). Let me know if you want me to post it tomorrow.

Ok thanks for the suggestions. I will try to do that.

Also in the Zombie class you could keep the following Properties:
Methods:

  • DrawMe (Method)

Properties:

  • Top (Integer)
  • Left (Integer)
  • CurrentWidth (Integer)
  • CurrentHeight (Integer)
  • isSelected (Boolean)
  • ZombieImage (Picture)
  • ZombieLocked (Boolean)

This just gives you an idea of some properties you can add to implement certain features for your Zombie class that you would individually instantiate for the number of Zombies you want running around :slight_smile:

That would be really helpful if you could do that for me. I’ve been having trouble with the animation because I couldn’t get the computer to animate all the zombies at once. It would start and stop because it would move on to a different zombie. I’m doing this for my final project for my intro to programming class and I only have two weeks left and a lot left to do. So I’ll take all the help I can get

Basically all I need the zombies to do are follow your character, attack, die, and animate

I didn’t learn how to use classes in my programming class because it is a really basic class so I’m a little shaky on them. I’ve been trying to learn on my own but I’m still a little confused

Check out the Sample Project “Objects in Canvas” Under XOjo New Project --> Graphics and Multimedia --> Objects in Canvas.

This will give you the jumpstart you will need to create the Class which is bacically the Zombie base class. You then “Instantiate” or create a copy of that Zombie class with unique properties for each new (instantiated) zombie. Ie. The position of that zombie (top/left) etc.

Thank you guys so much for all the suggestions. You guys have been a great help. I will try your guys suggestions

I’ll post the class when I have time tomorrow.

I really could use that code so please remember to get it to me as soon as possible. Thanks!

Sorry this took so long, I was much busier than expected today. Here is an example with the animated object class. When you create it, set the left, top, width, and height. Use the SetImages function with an array of images for the images used in the animation, or add each image individually with the AppendImage function. Then set AnimationFPS to the number of images to change/display every second, and finally enable the animation by setting AnimationEnabled to true. Then you only need a canvas to draw the object using the Draw method, along with a timer to Invalidate the canvas.

Once you have created an animated object you can call the GetJSON function which will return a string representing the object. This string can be passed as a parameter in the constructor to recreate the object exactly as before (maybe you will need template zombies which could be stored as constants or in files then easily created from the constant/file).

The DrawnObject base class doesn’t technically have to be a separate class from AnimatedObject however I needed them to be separate in my project and didn’t take the time to combine the methods and properties into the AnimatedObject class. If you have any questions, let me know. Good luck!

Thank you so much for all your help!

Is there a command that will activate code every frame or so? Or maybe a timer inside of a class? I looked at your code you gave me and I am pretty confused because I have not really used classes at all and barely even understand how they work.

The method “TimerAction” is connected to an internal timer with AddHandler and is called every frame.

Outline of the methods:
AppendImage accepts a single images and adds it to the array to be animated. SetImages accepts an array of images and makes them the array of images to be animated. Draw returns the image for the current frame so that it can be drawn on screen. The constructor is overloaded so that it will accept a string from a JSONItem and recreate the object from that string, or it will create a new AnimatedObject if no string is passed. GetJSON returns a string representing the object which could be later passed into the constructor or a new AnimatedObject to recreate the object.

Hopefully this helps

Checkout the SimDesignerCanvas…its free to try it out with no limitations. You can create zombies in “virtual” canvasas and you can reference each object separately and give each object user defined properties as well as manipulate using left top height width and mouse events. …its available in the addons category as well as xojodevspot.com. I’m even glad to assist in giving a crash course and demos on game development and collision detection.