Showing posts with label tip. Show all posts
Showing posts with label tip. Show all posts

Thursday, February 10, 2011

Tip of the day: self-executing anonymous functions

As you start digging into some serious JavaScript source code like jQuery, for instance, or when you begin writing a complex JavaScript application or a framework, you will no doubt get to see the following piece of code quite often.



So what is it exactly? As the title suggests, it is called a self-executing anonymous function. What it does is call itself immediately when the script that contains it gets loaded. In other words, if you need something that should execute automatically when the page gets loaded, this is the way - just wrap the instructions in such a function, and you will be sure that they will get executed.

Another common use case for that pattern is protecting your scripts from being interfered by other JavaScript code. Also, one can never be sure that the code that he or she writes is always using the proper instances, or whether some other JavaScript code has not messed up with the global scope variables. JavaScript can be very permissible on many occasions, and even stuff like "undefined = true" gets allowed. You can estimate yourself its impact on your code, unless you have declared what "undefined" is at the very beginning.

John Resig's description of self-executing anonymous funcions

In such cases, a self-executing anonymous function, instantiated with the proper instances of "window", "document", and "undefined" can be very valuable, although they are not mandatory

Again, simply wrap your entire script in such a function, and you can be sure that no external code is messing around with it.

Monday, January 17, 2011

Unity3D Tip: Fixing Skybox Textures

A skybox/skydome is a method of creating backgrounds to make computer and video games level look bigger than they really are. When a skybox is used, the level is enclosed in a cube/sphere, and the sky, distant mountains, distant buildings, and other unreachable objects are projected onto the cube's/sphere's surface. Wikipedia

I was recently working on creating a skybox for a demo level in Unity3D. I had all the textures (Front,Back,Left,Right,etc...) properly cut out and everything, but every single time I ran the level, the skybox looked patched, as if there was a seam between any two texture tiles. I think that it is a pretty common problem with newbies, and the solution is pretty simple. When all the textures that are going to be on the skybox are referenced in the project, one has to manually go through each one, and change its "wrap" property to "Clamp"

The general info on how to deal with skyboxes, can be found in the Unity3D FAQ, where I found the answer.

That's it. Pretty simple, yet bugging.

Thursday, July 15, 2010

Add Code Snippets with Github's Gist

I’ve been looking for a simple way to add formatted code snippets to my blog. I know that Wordpress has a few neat plugins that do the job, but because this is a Blogspot-based blog, I had to find an outside solution (or maybe there’s a way to it here, but I just don’t have the time and efforts to delve into that right now)

Well, I should say I found it. Plus, it’s not a miserable, banner-full service, but good old Github. Interestingly enought, the guys offer a nice way to make, share, fork, and embed code snippets in proper format, at the expense of adding a reference to a small js wherever the code should appear …. like that
<script src="”https://gist.github.com/765326.js?file=Test.cs”">
I think that the end result is good enough for sharing code snippets around.



Friday, February 12, 2010

Newbie tip: Declaring and Managing Linked MovieClip Instances in Parent Classes

Suppose that we have two MovieClip-based classes called MegaNode and SimpleNode. We have created both of them by drawing two vector shapes on the scene and converting them to those Movieclip-based classes. For some reason, we want to call the instance of the shape that's inside - mainShape, therefore we convert it to a MovieClip, and call the instance mainShape.

Now, since on many occasions the MovieClip class is too general for our purposes, we define a SuperNode class, which extends MC, and servers as the parent for both M
egaNode and SimpleNode. We need to be able to manipulate the mainShape instance, so we instantiate it in the SuperNode class:

public var mainShape:MovieClip;

if we simply do that, the compiler will throw the following error: "1152: A conflict exists with inherited definition SuperNode.mainShape in namespace public."
The reason for that is that by default, all instances of linked MovieClips, such as mainShape, are automatically declared by default at runtime. In order to make sure that we don't get into that error again, we need to go to the ActionScript s
ettings, and uncheck the "Automatically declare stage instances" checkbox. With that done, the above mentioned code will work, and we'll be able to manipulate the mainShape instance from the SuperNode class

NOTE: Make sure that instances like mainShape are always declared as public


Friday, September 26, 2008

Linux Newbie Tip: Starting GUI apps from the command line

Under the Linux command line shell, I want to be able to start GUI applications, without keeping the console window dependent on the application. If I simply type the application name, and press Enter, the application will run, but the console window will be unusable until I close the app. Moreover, if I close the console window, the GUI app dies with it.

The solution is to start the application as a background process. This is simply done by putting an ampersand ("&") after the command name like that:
nautilus &
emacs &
xmms &