Roenes
%Europe/Berlin %453 %2005, 11:52
Hey all,
Om de drempel om hier te posten wat te verlagen voor sommige mensen heb ik eens in me oude flashzooi gekeken of daar nog iets leuks stond. Daar kwam ik een oude Snake variant tegen die helemaal in AS gemaakt is. Met oud bedoel ik dan het proto tijdperk ;)
Nou weet ik dat aan deze versie qua code zat mankeert. Het kan nu velen malen beter. Zowel in AS1 als AS2. Ook kan ie nog een heel eind geoptimaliseerd worden. Nou wilde ik dat eens een x niet zelf doen maar jullie de kans geven. Ga eens lekker aan het prutsen en kijk eens wat je ervan kan maken. Zoiezo kan het in een stuk minder regels. Of je AS1 of AS2 gebruikt maakt me niet uit. :)
Aan de wat gevorderden scripters zou ik willen vragen om in eerste instantie geen (aangepaste) versie hier neer te zetten want ik weet dat het voor jullie een makkie is. Maar als zo'n versie er staat dan is het voor de iets minder gevorderde scripter misschien afschrikwekkend om zijn/haar versie te tonen. Uiteraard kunnen de "gevorderden" wel tips geven ed aan anderen. Je moet zelf maar zien tot welke groep je je rekend. Dat doe ik niet ;)
Najah, lang zat geluld! (mag vast niet door de woordfilter :D) Hier is de code:
Je moet de swf publishen naar FlashPlayer 6 aangezien FlashPlayer 7 geen prototypes meer ondersteund. Sorry voor dit ongemak :)
/* movie settings */
fscommand("allowscale", false);
fscommand("showmenu", false);
/* the needed vars */
field = { width:250, height:250 };
square = { width:10, height:10 };
score = 0;
speed = 10;
tail = 0;
snakePos = new Array();
tFF = new TextFormat("Verdana", 10, 0xFFFFFF, false, false, false, "", "", "center");
/* prototype for creating squares */
MovieClip.prototype.drawSquare = function(col, al, w, h)
{
this.beginFill(col, al);
this.lineTo(w, 0);
this.lineTo(w, h);
this.lineTo(0, h);
this.lineTo(0, 0);
this.endFill();
}
/* prototype for creating circles */
MovieClip.prototype.drawCircle = function(col, al, x, y, r)
{
this.beginFill(col, al);
this.moveTo(x-r, y);
this.curveTo(x-r, y-r, x, y-r);
this.curveTo(x+r, y-r, x+r, y);
this.curveTo(x+r, y+r, x, y+r);
this.curveTo(x-r, y+r, x-r, y);
this.endFill();
}
/* prototype for placing an mc at a random position */
MovieClip.prototype.randomPos = function(fw, fh, sw, sh)
{
var stringX = Math.round(Math.random() * (fw-sw)).toString();
var stringY = Math.round(Math.random() * (fh-sh)).toString();
var tempX = stringX.slice(0, stringX.length-1).concat(0);
var tempY = stringY.slice(0, stringY.length-1).concat(0);
var sP = this._parent.snakePos;
var sPLen = sP.length
for(var i = 0; i <= sPLen; ++i)
{
if(tempX == sP[i][0] && tempY == sP[i][1])
{
this.randomPos(field.width, field.height, square.width, square.height);
break;
}
else
{
this._x = tempX;
this._y = tempY;
}
}
}
/* prototype for a hittest between the head and the body of the snake */
MovieClip.prototype.selfHitTest = function()
{
var sP = this._parent.snakePos;
var sPLen = sP.length;
for(var i = 1; i < sPLen; ++i)
{
if(this._x == sP[i][0] && this._y == sP[i][1])
{
this.onEnterFrame = function()
{
this._alpha -= 5;
for(var i = 0; i < this._parent.tail; ++i)
{
var name = "tail"+i;
this._parent[name]._alpha -= 5;
}
if(this._alpha <= 0)
{
this.onEnterFrame = null;
}
}
}
}
}
/* prototype for a hittest between the head of the snake and the spot */
MovieClip.prototype.spotHitTest = function()
{
if(this._x == this._parent.head._x && this._y == this._parent.head._y)
{
var name = "tail"+tail;
++tail;
this._parent.createEmptyMovieClip(name, ++depth);
this._parent[name].drawSquare(0x00FF66, 100, square.width, square.height);
this._parent.snakePos[tail] = [this._x, this._y];
this._parent.score += 10;
this._parent.scoreboard.scoreTF.text = "Score is: " + this._parent.score;
this._parent.scoreboard.scoreTF.setTextFormat(tFF) ;
this.randomPos(field.width, field.height, square.width, square.height);
}
}
/* prototype for placing the head to the other side of the field when necessary */
MovieClip.prototype.moveOtherSide = function()
{
var headLowX = this._x;
var headHighX = this._x + this._width;
var headLowY = this._y;
var headHighY = this._y + this._height;
if(headLowX < 0) this._x = field.width-square.width;
if(headHighX > field.width) this._x = 0;
if(headLowY < 0) this._y = field.height-square.height;
if(headHighY > field.width) this._y = 0;
}
/* prototype for showing the scores */
MovieClip.prototype.showScores = function()
{
}
/* prototype for adding a score */
MovieClip.prototype.addScore = function()
{
}
/* the init function */
function init()
{
p = this.createEmptyMovieClip("playArea", ++depth);
p.drawSquare(0x0099FF, 100, field.width, field.height);
h = this.createEmptyMovieClip("head", 99999);
h.drawSquare(0x00FF66, 100, square.width, square.height);
e = h.createEmptyMovieClip("eyes", ++depth);
e.drawCircle(0x000000, 100, -square.width/4, 0, square.width/10);
e.drawCircle(0x000000, 100, square.width/4, 0, square.width/10);
e._x = square.width/2;
e._y = square.height/2;
h._x = field.width/2 - square.width/2;
h._y = field.height/2 - square.height/2;
s = this.createEmptyMovieClip("spot", ++depth);
s.drawCircle(0xFF0000, 75, 5, 5, 4);
s.moveTo(5, 3);
s.lineStyle(1.75, 0x000000, 100);
s.curveTo(5, 0, 2, 0);
s.randomPos(field.width, field.height, square.width, square.height);
s = this.createEmptyMovieClip("scoreboard", ++depth);
s._y = field.height;
s.drawSquare(0x0066FF, 75, field.width, 50);
s.createTextField("scoreTF", ++depth, 0, 0, 0, 0);
s.scoreTF.autoSize = true;
s.scoreTF.selectable = false;
s.scoreTF.text = "Score is: " + score;
s.scoreTF.setTextFormat(tFF);
s.createTextField("pauseTF", ++depth, 0, 35, 0, 15);
s.pauseTF.selectable = false;
s.pauseTF.autoSize = true;
s.pauseTF.text = "Press P for pause";
s.pauseTF.setTextFormat(tFF);
}
init();
/* watching which key is pressed */
kL = new Object();
kL.onKeyDown = function()
{
var tempKey = Key.getCode();
if(tempKey == 80 || tempKey == 37 || tempKey == 38 || tempKey == 39 || tempKey == 40) keyCode = tempKey;
}
Key.addListener(kL);
/* moving of the snake */
head.onEnterFrame = function()
{
/* P for pause */
if(keyCode != 80)
{
snakePos[0] = [this._x, this._y];
for(var i = 0; i <= this._parent.snakePos.length-1; ++i)
{
snakePos[i+1][0] = this._parent["tail"+i]._x;
snakePos[i+1][1] = this._parent["tail"+i]._y;
}
/* left arrow */
if(keyCode == 37 && dir != "right")
{
this._x -= speed;
this.eyes._rotation = 90;
dir = "left";
}
else if(keyCode == 37 && dir == "right") this._x += speed;
/* right arrow */
if(keyCode == 39 && dir != "left")
{
this._x += speed;
this.eyes._rotation = 90;
dir = "right";
}
else if(keyCode == 39 && dir == "left") this._x -= speed;
/* up arrow */
if(keyCode == 38 && dir != "down")
{
this._y -= speed;
this.eyes._rotation = 0;
dir = "up";
}
else if(keyCode == 38 && dir == "down") this._y += speed;
/* down arrow */
if(keyCode == 40 && dir != "up")
{
this._y += speed;
this.eyes._rotation = 0;
dir = "down";
}
else if(keyCode == 40 && dir == "up") this._y -= speed;
this.selfHitTest();
this.moveOtherSide();
spot.spotHitTest();
for(var i = 0; i <= snakePos.length-1; ++i)
{
this._parent["tail"+i]._x = snakePos[i][0];
this._parent["tail"+i]._y = snakePos[i][1];
}
}
}
ps. niet lachen om mijn uitermate mooie (met as getekende) snake en appeltje ;)
Om de drempel om hier te posten wat te verlagen voor sommige mensen heb ik eens in me oude flashzooi gekeken of daar nog iets leuks stond. Daar kwam ik een oude Snake variant tegen die helemaal in AS gemaakt is. Met oud bedoel ik dan het proto tijdperk ;)
Nou weet ik dat aan deze versie qua code zat mankeert. Het kan nu velen malen beter. Zowel in AS1 als AS2. Ook kan ie nog een heel eind geoptimaliseerd worden. Nou wilde ik dat eens een x niet zelf doen maar jullie de kans geven. Ga eens lekker aan het prutsen en kijk eens wat je ervan kan maken. Zoiezo kan het in een stuk minder regels. Of je AS1 of AS2 gebruikt maakt me niet uit. :)
Aan de wat gevorderden scripters zou ik willen vragen om in eerste instantie geen (aangepaste) versie hier neer te zetten want ik weet dat het voor jullie een makkie is. Maar als zo'n versie er staat dan is het voor de iets minder gevorderde scripter misschien afschrikwekkend om zijn/haar versie te tonen. Uiteraard kunnen de "gevorderden" wel tips geven ed aan anderen. Je moet zelf maar zien tot welke groep je je rekend. Dat doe ik niet ;)
Najah, lang zat geluld! (mag vast niet door de woordfilter :D) Hier is de code:
Je moet de swf publishen naar FlashPlayer 6 aangezien FlashPlayer 7 geen prototypes meer ondersteund. Sorry voor dit ongemak :)
/* movie settings */
fscommand("allowscale", false);
fscommand("showmenu", false);
/* the needed vars */
field = { width:250, height:250 };
square = { width:10, height:10 };
score = 0;
speed = 10;
tail = 0;
snakePos = new Array();
tFF = new TextFormat("Verdana", 10, 0xFFFFFF, false, false, false, "", "", "center");
/* prototype for creating squares */
MovieClip.prototype.drawSquare = function(col, al, w, h)
{
this.beginFill(col, al);
this.lineTo(w, 0);
this.lineTo(w, h);
this.lineTo(0, h);
this.lineTo(0, 0);
this.endFill();
}
/* prototype for creating circles */
MovieClip.prototype.drawCircle = function(col, al, x, y, r)
{
this.beginFill(col, al);
this.moveTo(x-r, y);
this.curveTo(x-r, y-r, x, y-r);
this.curveTo(x+r, y-r, x+r, y);
this.curveTo(x+r, y+r, x, y+r);
this.curveTo(x-r, y+r, x-r, y);
this.endFill();
}
/* prototype for placing an mc at a random position */
MovieClip.prototype.randomPos = function(fw, fh, sw, sh)
{
var stringX = Math.round(Math.random() * (fw-sw)).toString();
var stringY = Math.round(Math.random() * (fh-sh)).toString();
var tempX = stringX.slice(0, stringX.length-1).concat(0);
var tempY = stringY.slice(0, stringY.length-1).concat(0);
var sP = this._parent.snakePos;
var sPLen = sP.length
for(var i = 0; i <= sPLen; ++i)
{
if(tempX == sP[i][0] && tempY == sP[i][1])
{
this.randomPos(field.width, field.height, square.width, square.height);
break;
}
else
{
this._x = tempX;
this._y = tempY;
}
}
}
/* prototype for a hittest between the head and the body of the snake */
MovieClip.prototype.selfHitTest = function()
{
var sP = this._parent.snakePos;
var sPLen = sP.length;
for(var i = 1; i < sPLen; ++i)
{
if(this._x == sP[i][0] && this._y == sP[i][1])
{
this.onEnterFrame = function()
{
this._alpha -= 5;
for(var i = 0; i < this._parent.tail; ++i)
{
var name = "tail"+i;
this._parent[name]._alpha -= 5;
}
if(this._alpha <= 0)
{
this.onEnterFrame = null;
}
}
}
}
}
/* prototype for a hittest between the head of the snake and the spot */
MovieClip.prototype.spotHitTest = function()
{
if(this._x == this._parent.head._x && this._y == this._parent.head._y)
{
var name = "tail"+tail;
++tail;
this._parent.createEmptyMovieClip(name, ++depth);
this._parent[name].drawSquare(0x00FF66, 100, square.width, square.height);
this._parent.snakePos[tail] = [this._x, this._y];
this._parent.score += 10;
this._parent.scoreboard.scoreTF.text = "Score is: " + this._parent.score;
this._parent.scoreboard.scoreTF.setTextFormat(tFF) ;
this.randomPos(field.width, field.height, square.width, square.height);
}
}
/* prototype for placing the head to the other side of the field when necessary */
MovieClip.prototype.moveOtherSide = function()
{
var headLowX = this._x;
var headHighX = this._x + this._width;
var headLowY = this._y;
var headHighY = this._y + this._height;
if(headLowX < 0) this._x = field.width-square.width;
if(headHighX > field.width) this._x = 0;
if(headLowY < 0) this._y = field.height-square.height;
if(headHighY > field.width) this._y = 0;
}
/* prototype for showing the scores */
MovieClip.prototype.showScores = function()
{
}
/* prototype for adding a score */
MovieClip.prototype.addScore = function()
{
}
/* the init function */
function init()
{
p = this.createEmptyMovieClip("playArea", ++depth);
p.drawSquare(0x0099FF, 100, field.width, field.height);
h = this.createEmptyMovieClip("head", 99999);
h.drawSquare(0x00FF66, 100, square.width, square.height);
e = h.createEmptyMovieClip("eyes", ++depth);
e.drawCircle(0x000000, 100, -square.width/4, 0, square.width/10);
e.drawCircle(0x000000, 100, square.width/4, 0, square.width/10);
e._x = square.width/2;
e._y = square.height/2;
h._x = field.width/2 - square.width/2;
h._y = field.height/2 - square.height/2;
s = this.createEmptyMovieClip("spot", ++depth);
s.drawCircle(0xFF0000, 75, 5, 5, 4);
s.moveTo(5, 3);
s.lineStyle(1.75, 0x000000, 100);
s.curveTo(5, 0, 2, 0);
s.randomPos(field.width, field.height, square.width, square.height);
s = this.createEmptyMovieClip("scoreboard", ++depth);
s._y = field.height;
s.drawSquare(0x0066FF, 75, field.width, 50);
s.createTextField("scoreTF", ++depth, 0, 0, 0, 0);
s.scoreTF.autoSize = true;
s.scoreTF.selectable = false;
s.scoreTF.text = "Score is: " + score;
s.scoreTF.setTextFormat(tFF);
s.createTextField("pauseTF", ++depth, 0, 35, 0, 15);
s.pauseTF.selectable = false;
s.pauseTF.autoSize = true;
s.pauseTF.text = "Press P for pause";
s.pauseTF.setTextFormat(tFF);
}
init();
/* watching which key is pressed */
kL = new Object();
kL.onKeyDown = function()
{
var tempKey = Key.getCode();
if(tempKey == 80 || tempKey == 37 || tempKey == 38 || tempKey == 39 || tempKey == 40) keyCode = tempKey;
}
Key.addListener(kL);
/* moving of the snake */
head.onEnterFrame = function()
{
/* P for pause */
if(keyCode != 80)
{
snakePos[0] = [this._x, this._y];
for(var i = 0; i <= this._parent.snakePos.length-1; ++i)
{
snakePos[i+1][0] = this._parent["tail"+i]._x;
snakePos[i+1][1] = this._parent["tail"+i]._y;
}
/* left arrow */
if(keyCode == 37 && dir != "right")
{
this._x -= speed;
this.eyes._rotation = 90;
dir = "left";
}
else if(keyCode == 37 && dir == "right") this._x += speed;
/* right arrow */
if(keyCode == 39 && dir != "left")
{
this._x += speed;
this.eyes._rotation = 90;
dir = "right";
}
else if(keyCode == 39 && dir == "left") this._x -= speed;
/* up arrow */
if(keyCode == 38 && dir != "down")
{
this._y -= speed;
this.eyes._rotation = 0;
dir = "up";
}
else if(keyCode == 38 && dir == "down") this._y += speed;
/* down arrow */
if(keyCode == 40 && dir != "up")
{
this._y += speed;
this.eyes._rotation = 0;
dir = "down";
}
else if(keyCode == 40 && dir == "up") this._y -= speed;
this.selfHitTest();
this.moveOtherSide();
spot.spotHitTest();
for(var i = 0; i <= snakePos.length-1; ++i)
{
this._parent["tail"+i]._x = snakePos[i][0];
this._parent["tail"+i]._y = snakePos[i][1];
}
}
}
ps. niet lachen om mijn uitermate mooie (met as getekende) snake en appeltje ;)