Sticky Thread && Your Source Code

18 posts

Flag Post

two things.

Could you guys offer up your cleanest, most flushed out, most efficient old source codes? I’m trying to strengthen these things and my design patterns.
The more I program lately, the more I’m realizing just how sloppy it is. Its like I see the world with brand new eyes.

Secondarily, We should Sticky a thread that contains a link to all the threads we would like to sticky.
Theres a LOT of helpful threads out there, but its very easy for them to get buried, and quickly.
For example, oatlol’s tutorials would be very beneficial to those who are looking for the creme de le creme of tutorials.

One thread to sticky them all. One thread to find them, and in the kongness bind them.

 
Flag Post
Originally posted by alecz127:

Could you guys offer up your cleanest, most flushed out, most efficient old source codes?

Efficient at doing what, exactly? ;)

 
Flag Post

Anything.
I just want to see well organized code structures. Maybe really good ways of linking .as files together.
Bottom line, anything.

 
Flag Post

Originally posted by alecz127:

Anything.

this is efficient at heating the room:

while(1);
 
Flag Post

I’m trying to think of something real hard beut i think you should be a bit more detailed with your request :)

 
Flag Post

I like your sticky a thread that has other threads linked. I hope they sticky the thread tho.

 
Flag Post

If you want to see pretty, organized structures that don’t serve any particular purpose I recommend programming textbooks.

 
Flag Post

I will not share my fastest source codes, they are trade secrets.

 
Flag Post

Although vaguely phrased, there could be something useful along these lines, so this is the class I use to show fps when testing. It’s not that accurate, but good enough to tell whether it’s lagging & whether this build lags more or less than the last… All credit goes to wherever I got it from, I fiddled with it a bit, but can’t remember what I changed. It’s a long way from rocket science anyways…

package {
        import flash.events.Event;
        import flash.display.Sprite;
	import flash.events.TimerEvent;
        import flash.text.TextField;
	import flash.utils.Timer;
        public class FramerateTracker extends Sprite    {
                private var fps_txt:TextField;
		private var FramesThisSecond:int;
		private var OneSecondOfTimeOnlyInTheory:Timer = new Timer(1000);
                public function FramerateTracker()        {
			OneSecondOfTimeOnlyInTheory.addEventListener(TimerEvent.TIMER, second);
			OneSecondOfTimeOnlyInTheory.start();
                        fps_txt = new TextField();
			fps_txt.width = 100;
			fps_txt.height = 20;
			fps_txt.mouseEnabled = false;
			fps_txt.textColor = 0xFF0000;
                        addEventListener(Event.ENTER_FRAME, countFps);  
			addChild(fps_txt);
                }
                private function countFps(e:Event):void        {
			FramesThisSecond += 1;
                }  
		private function second(event:TimerEvent):void {
			fps_txt.text = "fps: " + FramesThisSecond;
			FramesThisSecond = 0; 
		}
        }  
} 
 
Flag Post

I am a bit confused, why do you need to extend Sprite? I can’t see any Sprite functionality being used in any of your code.

 
Flag Post
Originally posted by Drakim:

I am a bit confused, why do you need to extend Sprite? I can’t see any Sprite functionality being used in any of your code.

It’s added to the display list. So it needs to extend SOME display object, and Sprites are the cheapest.

 
Flag Post

I don’t use the native display list classes very often, but couldn’t he append the TextField directly to the stage?

 
Flag Post
Originally posted by Drakim:

I don’t use the native display list classes very often, but couldn’t he append the TextField directly to the stage?

Encapsulation.

 
Flag Post
Originally posted by skyboy:

Originally posted by alecz127:

Anything.

this is efficient at heating the room:

while(1);

I am going to be laughing for fucking days because of this.

 
Flag Post

Yeah that was a good one, if old.

Not code exactly, but since you talked about patterns: I’ve taken to implementing game objects with a strict model/view separation, i.e. have a collection of game objects which have no concept of rendering except a reference to a View (which in a 2D scene can be a DisplayObject, in a 3D world it would be surfaces to be fed to a rendering pipeline). Then you can have a LevelView which handles drawing and a Level which handles the actual interaction.

Another pattern I use is to give objects a controller if they are movable. This can be from the keyboard and mouse, or from AI instructions. Having separate controller classes makes it really easy to switch between different characters, or have computer control of some objects.

Put together these two ideas make up the ‘MVC’ (model-view-controller) approach and they can help a lot.

 
Flag Post

This thread is a good idea.

For fast sourcecode, just ask Microsoft. They have the fastest, cleanest code of anyone! :D

 
Flag Post
Originally posted by Drakim:

I will not share my fastest source codes, they are trade secrets.

Here’s my fastest source code:

package {
	import flash.display.Sprite;
	public class Main extends Sprite {
		public function Main():void {
		}
	}
}

It takes virtually no time at all!

 
Flag Post

well this is just some neat way to check neighbours on a grid that I’ve grown fond of doing. Actually, there’s 2 tricks in 1 here. Written in C++ because my AS3 syntax has gotten rusty :/ Done as a BFS for example, but really applies to whatever (for example A* on grid)

int dd[5] = {0,1,0,-1,0};

// Takes a square grid of . and #, and returns the length of the shortest path from (sr, sc) to (tr, tc)
// Assumes (sr, sc) and (tr, tc) are within the grid and are . tiles. If no path exists returns -1.
int bfs(int n, char grid[n][n], int sr, int sc, int tr, int tc) {
    // Resize grid and pad the sides with #
    char g[n+2][n+2];
    for (int i=0; i < n+2; i++) g[0][i]=g[i][0]=g[n+1][i]=g[i][n+1]='#';
    for (int i=0; i < n; i++) for (int j=0; j < n; j++) g[i+1][j+1] = grid[i][j];

    // BFS distance array
    int dist[n][n]; for (int i=0; i < n; i++) for (int j=0; j < n; j++) dist[i][j] = n*n;
    dist[sr][sc] = 0;

    // Queue of what to expand next
    queue<pair<int, int> > q;
    q.push(make_pair(sr, sc));
    while(!q.empty()) {
        int r = q.front().first, c = q.front().second; q.pop();
        if (r == tr && c == tc) return dist[r][c];
        for (int i=0; i < 4; i++) {
          int rr = r + dd[i], cc = c + dd[i];
          if (g[rr][cc]=='.' && dist[rr][cc] > dist[r][c]+1) {
            dist[rr][cc] = dist[r][c]+1;
            q.push(make_pair(rr, cc));
          }
        }
    }
    return -1;
}