Jump to content

Drongo_III

Members
  • Posts

    579
  • Joined

  • Last visited

Everything posted by Drongo_III

  1. Hi Guys I don't know whether this is possible or not but I've been going down a lot of dead ends for a few hours now and I'm ready to defer to a higher power... I want to create a function that will accept an array of keys as it's first argument and a value as it's second argument and basically add the keys in a multidimensional format to produce an array with the data as its final value. So if I passed into the function: someFunc(array('key1','key2'), 'data'); I would end up with an array that looks like: [key1][key2] = 'data' I've been trying to do it with a while loop but I become unstuck in adding the subsequent layers to the array. I've also explored using the 'end' and 'key' method hoping I could just specify the pointer and add an element there but there doesnt appear to be anything like that. Any help is very welcome! Dronogo
  2. Thanks for the advice!
  3. Yeah I could use a relative path but on other servers I've worked on they've set them up so you just use /includes/file-name.php and it doesnt matter where you then make the call from it still works. I guess I was just trying to work out how to configure xampp so it always looks at /htdocs/ as it's root when including files?
  4. Hello Hope someone can help and I'm sure this is a simple one. I'm using a local copy of XAMPP at the moment and have a basic issue but I've been searching around and struggling to find the answer. Usually when I work on a site, lets say it's domain is example.com, and I wish to include a file from a location like example.com/includes/, I would simply use a php's include as follows: <?php include('/includes/some-file.php'); ?> In this instance I'd expect the '/' to refer to the base domain - i.e. example.com - so that irrespective of where I include the file from it always has a sound reference. However, on my local xampp i'm having issues. So lets say I have a file I want to include located in in: /htdocts/ng/includes/some-file.php And I try to include it from a sub directory /htdocs/ng/some-dir/ as per the code posted above I get an error: "No such file or directory in C:\xampp\htdocs\ng\some-dir\some-file.php" It's as if the include path is always trying to include from the current directory. So is there a setting in apache I need to change to ensure that the base domain is always referenced? Hope that makes sense, Drongo
  5. Scoot you're a genius sir! I hadn't considered this was symptomatic of the way the OS handles repeating keys. Your post got me to searching out how to overcome the delay and I found a solution along the lines of what you proposed. Essentially you have a loop monitoring a keyState object to determine whether a key is up or down and then it reacts accordingly. The solution also means multiple key presses can be used at once. The code is below if anyone experiences the same problem: <!doctype html> <html> <head> <style type="text/css"> canvas { border: 1px solid #888; } </style> <script> window.onload = function(){ //initialise canvas and context c.init(); window.addEventListener('keydown', function(e){ keyState[e.keyCode || e.which] = true; }, false); window.addEventListener('keyup', function(e){ keyState[e.keyCode || e.which] = false; }); } //holds key state var keyState = {} //canvas var c = { cvs : null, //canvas element ctx : null, //context pos : {y:0,x:0}, inc : 5, init : function(){ //create canvas obj and context this.cvs = document.getElementById('canvas1'); this.ctx = this.cvs.getContext('2d'); //draw initial rectangle at starting point c.ctx.fillStyle = 'black'; c.ctx.fillRect(0, 0, 60, 60); //start keystroke monitoring this.loopPosition(); }, animate: function(){ //clear the canvas this.clearContext(); //redraw in new position this.ctx.fillStyle = 'black'; this.ctx.fillRect(this.pos.x, this.pos.y, 60, 60); }, clearContext : function(){ c.ctx.clearRect(0,0,this.cvs.width,this.cvs.height); }, loopPosition: function(){ if(keyState[37]){ this.pos.x-=1; } if(keyState[39]){ this.pos.x+=1; } if(keyState[40]){ //down this.pos.y+=1; } if(keyState[38]){ //up this.pos.y-=1; } //then animate this.animate(); //loop the method setTimeout(this.loopPosition.bind(c),10); } } </script> </head> <body> <canvas id="canvas1" width="500" height="500"></canvas> </body> </html>
  6. Hi I'm testing out animation using canvas.. The code below just animates a square in response to holding down one of the arrow keys. The problem is when I hold down an arrow key there appears to be a short delay before the square starts animating. I'm guessing it's something to do with the keydown event firing rapidly but I'm not sure. So if anyone has any suggestions as to why this might be happening it would help me greatly! The desired effect is for the square to animate immediately without any delay. Thanks, Drongo <!doctype html> <html> <head> <style type="text/css"> canvas { border: 1px solid #888; } </style> <script> window.onload = function(){ //initialise canvas and context c.init(); //set event handler for keydown window.addEventListener('keydown', c.setPosition.bind(c)); } //canvas var c = { cvs : null, //canvas element ctx : null, //context pos : {y:0,x:0}, inc : 5, init : function(){ //create canvas obj and context this.cvs = document.getElementById('canvas1'); this.ctx = this.cvs.getContext('2d'); //draw initial rectangle at starting point c.ctx.fillStyle = 'black'; c.ctx.fillRect(0, 0, 60, 60); }, animate: function(){ //clear the canvas this.clearContext(); //redraw in new position this.ctx.fillStyle = 'black'; this.ctx.fillRect(this.pos.x, this.pos.y, 60, 60); }, clearContext : function(){ c.ctx.clearRect(0,0,this.cvs.width,this.cvs.height); }, setPosition : function(e){ //increment position based on key console.log(e.keyCode); switch(e.keyCode){ case 40: this.pos.y += this.inc; break; case 38: this.pos.y -= this.inc; break; case 37: this.pos.x -= this.inc; break; case 39: this.pos.x += this.inc; break; } //then animate this.animate(); } } </script> </head> <body> <canvas id="canvas1" width="500" height="500"></canvas> </body> </html>
  7. I think I see what you mean. Thanks.
  8. Out of interest though can anyone explain why the window object works like that? How is the value of the variable gets used in that context?
  9. Found the answer. If this of any help to anyone you can call it by using the window object: var a = new window[p]();
  10. Hello Probably a really basic answer here but I'm stuck and google isn't bringing me any closer. Is it possible to dynamically create objects using a variable? For instance in PHP I might do: class turnip { public function __construct(){ echo 'created turnip'; } } $t = 'turnip'; $tt = new $t(); //echoes created turnip However, trying to do something similar in JS is simply causing an error. So is there a special trick? Lets say I wanted to do: function Pawn(){...} var p = 'Pawn'; var a = new p(); //Can this be done somehow? Can you force the variable 'p' to evaluate down to it's value?
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.