Creating a grid with an array

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
mystd
Posts: 13
Joined: Mon Jul 30, 2012 8:44 am

Creating a grid with an array

Post by mystd »

I'm currently working on an grid array that portions the canvas, but as beginner I'm kinda having some problems. Here is what I've made so far:

Code: Select all

//global variables I dont really wanna use but dont know how to solve it without
var screenPix = 0;
var screenTiles = 0;
var MapArray = new Array();


	MapArrayFunc: function(){
		screenPix = ig.system.realWidth * ig.system.realHeight;
		screenTiles = screenPix / 32;
		MapArray = new Array();
		for (i=0; i<=screenTiles; i++){
			MapArray[i]++;
		};//for end
	},//MapArrayFunc end

	draw: function() {
		// Draw all entities and backgroundMaps
		this.parent();
		
		//map array test[code]
this.font.draw(screenPix, 50, 50);
this.font.draw(screenTiles, 50, 60);
this.font.draw(MapArray[0], 50, 50);
}//draw end

[/code]

Code: Select all

The output is 0
              0
I realized that what I've made wont make a grid but I'm wondering why the output is 0 0 instead of screenPix = 307200, screenTiles 9600 and MapArray (9601, 9602 ...)

I've have changed it to:

Code: Select all

//Only 1 glob var left
var MapArray = new Array();

	MapArrayFunc: function(){
		screenX = 0;
		screenY = 0;
		for (i=0; i=(ig.system.realWidth*ig.system.realHeight)/32; i++){
			MapArray[screenX,screenY];
			screenX+32;
			screenY+32;
		};//for end
	},//MapArrayFunc end

draw: function() {
		// Draw all entities and backgroundMaps
		this.parent();
		
		//map array
		this.font.draw(screenPix, 50, 50);
		this.font.draw(screenTiles, 50, 60);
		this.font.draw(MapArray[0,1], 50, 50);
	}//draw end
Now it shows nothing so Im not sure if it works or not.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Creating a grid with an array

Post by Jackolantern »

When you use the format that Impact requires, you won't have any global variables: they are all local to the module they are in!

You are putting this code into Impact modules, correct? If not, all kinds of bad stuff will happen.
The indelible lord of tl;dr
mystd
Posts: 13
Joined: Mon Jul 30, 2012 8:44 am

Re: Creating a grid with an array

Post by mystd »

Oh ok need to reread the module, portion again.
-------
I use the modules I've just added my own code to it. I've read the module part again, but I dono if I understand it fully. My understanding of modules is that if you write one you can add to/change a working system without changing the core. So Impact works with different modules that do their own tasks and work together to form a frame work, am I correct with that? Since I am working inside of modules I cannot use global variables, that makes sense. But how do I use a variables data in a function inside of other functions then? Or do they become "module-global" to be used in all of the modules functions?
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Creating a grid with an array

Post by Jackolantern »

Yes, that is correct :)

And you don't want globals floating around your game, since you have no control over where or how they are accessed. The typical way to control and share variables between modules is by keeping those variables as part of a an entity class's variables, and then importing that entity through game.getEntityByName() or game.getEntitiesByType(). To use the getEntityByName (typically the one you want, since it fetches a specific entity), you need to give your entity a "name" property. You can do that like this (also showing the full module syntax):

Code: Select all

ig.module( 
    'game.player' 
)
.requires(
    'impact.game',
    'impact.image',
    'game.other-file'
)
.defines(function(){
// Create your own entity, subclassed from ig.Enitity
EntityPlayer = ig.Entity.extend({

    // Set some of the properties
    name: 'player',
    collides: ig.Entity.COLLIDES.ACTIVE,
    type: ig.Entity.TYPE.A,
    checkAgainst: ig.Entity.TYPE.B,

    size: {x: 16, y: 16},
    health: 50,
    
    // Load an animation sheet
    animSheet: new ig.AnimationSheet( 'media/player.png', 16, 16 ),
    
    init: function( x, y, settings ) {
        // Add animations for the animation sheet
        this.addAnim( 'idle', 0.1, [0,1,2] );
        this.addAnim( 'jump', 0.1, [3,4,5] );
        
        // Call the parent constructor
        this.parent( x, y, settings );
    }
    
    update: function() {
        // This method is called for every frame on each entity.
        // React to input, or compute the entity's AI here.
        
        if( ig.input.pressed('jump') ) {
            this.vel.y = -100;
            this.currentAnim = this.anims.jump.rewind();
        }
        
        // Call the parent update() method to move the entity
        // according to its physics
        this.parent(); 
    }
});
    

});
You see that you define the class by extending Entity inside the .defines() part of the module. That is because the .defines() part of the module is the executable code of the module. Inside the class definition you are using Javascript object notation to create its functions and variables, which is why you have a name, a colon and a value or a function. The name is simply a string, but that will allow you to import the entity into other modules so you can call their functions and check or change their variables. Hope this helps! :)
The indelible lord of tl;dr
mystd
Posts: 13
Joined: Mon Jul 30, 2012 8:44 am

Re: Creating a grid with an array

Post by mystd »

Thx I understand it much better now, its a bit complicated to work with but Ill have to adjust to that ^-^
Last edited by mystd on Sat Aug 11, 2012 7:47 pm, edited 1 time in total.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Creating a grid with an array

Post by Jackolantern »

Once you get used to them and the naming conventions (which you can find here under the "Modules" section), you will see most of it is boilerplate code. You can basically save the basic module structure, and just start from there for your entities, just over-writing a couple of names and adding your module code into the class extension structure. Have you also gone through the Create a Game: Pong video tutorial on the Impact site? :)
The indelible lord of tl;dr
mystd
Posts: 13
Joined: Mon Jul 30, 2012 8:44 am

Re: Creating a grid with an array

Post by mystd »

Yes I read the whole documentation and watched all impact vids I found, but the info didn't stick to my brain too well XD And its different seeing someone to work with something than working yourself with it. I'm currently trying to figure out how everything works in more detail, Ill have to read the docs a couple of times more, but Ill combine it with some coding, so I'll stick better. Just reading it didn't work ^-^
I've coded a bit in school (15 years ago) with turbo pascal & qbasic and that old coding logic is making it a bit harder to adapt to object oriented/module based programming.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Creating a grid with an array

Post by Jackolantern »

Very true. It will take some time and experimenting to get used to it. Don't be afraid to look-up and reference the documentation while coding. Everyone does it, and almost constantly when starting ;)
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Creating a grid with an array

Post by hallsofvallhalla »

anyone ran into any limitations with JS and/or PHP arrays? Wondering how big one can really go.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Creating a grid with an array

Post by Jackolantern »

I have seen huge ones, with over several thousand values. There is no hard-coded limit as far as I know, nor should there be. However, the system the program runs on will eventually begin to impose limits, and your performance will start to be impacted as arrays grow very, very large. Eventually you will have to seriously consider every operation you make with it, because one poorly-thought-out passing of the array down a chain of functions could crash the whole system with an Out of Memory error (or crash the browser tab, virtual machine, etc. as is the case in some languages). There are entire academically-studied algorithms for dealing with huge arrays, such as the Binary Search algorithm. These are designed to keep the program from locking up when doing things such as iterating a huge array looking for a needle.

Basically, the only limitations are typically the system and your design. Obviously system limitations are going to come much faster for sandboxed runtimes, such as Javascript. 8-)
The indelible lord of tl;dr
Post Reply

Return to “Beginner Help and Support”