-
Can this be optimized?
Actionscript Code:
if (_root["playerMC"+2]._x<=newX) { _root["playerMC"+2]._x += speed; } if (_root["playerMC"+2]._x>=newX) { _root["playerMC"+2]._x -= speed; } if (_root["playerMC"+2]._y<=newY) { _root["playerMC"+2]._y += speed; } if (_root["playerMC"+2]._y>=newY) { _root["playerMC"+2]._y -= speed; }
Because I need to add tons of those... but with different ["names"]+2.
-
M.D.
for loop
Code:
for (var i:Number = 0; i < players; i++) {
var player:MovieClip = _root["playerMC" + i];
//do logic with player
if (player._x <= newX) ...
}
-
Pumpkin Carving 2008
Ternary maybe?
In a for loop you could run something like...
Code:
player._x += (player.x<=newX?speed:-speed);
The 'Boose':
ASUS Sabertooth P67 TUF
Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
8GB G.Skill Ripjaws 1600 DDR3
ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
New addition: OCZ Vertex 240GB SATA III SSD
WEI Score: 7.6
-
I always forget how to iterate over objects in AS so this might not be exactly right..
Code:
var movingObjects = {'playerMC','badGuyMC','playerMomMC'};
for(var k:String in movingObjects)
{
_root[k+2] ... blah blah blah
}
-
Pumpkin Carving 2008
Actually I miss-posted my first reply; it should be
Code:
player._x += ( player.x < newX ? speed : -speed );
Where it just checks < instead of <=. This is because in your example if the player.x is equal to newX, the two actions cancel each other out. Therefore we only need to check if it is less than (or greater than) and apply the right action, otherwise apply the opposite.
The 'Boose':
ASUS Sabertooth P67 TUF
Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
8GB G.Skill Ripjaws 1600 DDR3
ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
New addition: OCZ Vertex 240GB SATA III SSD
WEI Score: 7.6
-
Sorry, I'm a bit noobish and don't quite get the above examples, or how to make them work. So here's an example you can optimize. As I don't get it when you just give me a piece of code, but it's not the whole code as I need to do more stuff like putting it in a foor loop to work and stuff
Code:
this.onEnterFrame = function() {
if (_root.Action == "moving") {
if (_root["playerMC"+2]._x<=newX) {
_root["playerMC"+2]._x += speed;
}
if (_root["playerMC"+2]._x>=newX) {
_root["playerMC"+2]._x -= speed;
}
if (_root["playerMC"+2]._y<=newY) {
_root["playerMC"+2]._y += speed;
}
if (_root["playerMC"+2]._y>=newY) {
_root["playerMC"+2]._y -= speed;
}
//----------------------------------
if (_root["dogMC"+2]._x<=newX) {
_root["dogMC"+2]._x += speed;
}
if (_root["dogMC"+2]._x>=newX) {
_root["dogMC"+2]._x -= speed;
}
if (_root["dogMC"+2]._y<=newY) {
_root["dogMC"+2]._y += speed;
}
if (_root["dogMC"+2]._y>=newY) {
_root["dogMC"+2]._y -= speed;
}
//----------------------------------
if (_root["rabbitMC"+2]._x<=newX) {
_root["rabbitMC"+2]._x += speed;
}
if (_root["rabbitMC"+2]._x>=newX) {
_root["rabbitMC"+2]._x -= speed;
}
if (_root["rabbitMC"+2]._y<=newY) {
_root["rabbitMC"+2]._y += speed;
}
if (_root["rabbitMC"+2]._y>=newY) {
_root["rabbitMC"+2]._y -= speed;
}
//----------------------------------
if (_root["birdMC"+2]._x<=newX) {
_root["birdMC"+2]._x += speed;
}
if (_root["birdMC"+2]._x>=newX) {
_root["birdMC"+2]._x -= speed;
}
if (_root["birdMC"+2]._y<=newY) {
_root["birdMC"+2]._y += speed;
}
if (_root["birdMC"+2]._y>=newY) {
_root["birdMC"+2]._y -= speed;
}
}
};
-
Senior Member
Code:
objArray = ["playerMC","dogMC","rabbitMC","birdMC"];
this.onEnterFrame = function()
{
if(_root.Action=="moving")
{
for(i=0;i<objArray.length;i++)
{
_root[objArray[i]+"2"]._x+=_root[objArray[i]+"2"]._x>=newX?speed:~speed-1;
_root[objArray[i]+"2"]._y+=_root[objArray[i]+"2"]._y>=newY?speed:~speed-1;
}
}
}
I am writing this from mind, and I have forgotten most of as2, so there might be some silly mistakes, but that is the general idea
If you like me, add me to your friends list .
PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!
My Arcade My Blog
Add me on twitter:
-
Pumpkin Carving 2008
Sorry, in my example, I use a ternary operation. This is a simply a condensed (i.e. slightly faster, as far as I know) version of an if else statement. What this can help you do is "clean" your code by putting the entire if else statement on one line which makes it much more readable and maintainable.
On a side note, not really sure what's up with the tilde's in bluemagica's example above because like him I've forgotten most of my AS2 knowledge but the core examples are the same.
A ternary operation works like this:
Code:
expression ? returned value if expression is true : returned value if expression is false
In relation to a typical if-else statement it's treated like this:
Code:
if (expression) {
// if expression is true
return true-value
} else {
// if expression is false
return false-value
}
The last notable difference between bluemagica's example and my own is that I typically encase a ternary operation with parenthases because it helps clarify what is part of the ternary operation, but I also use it to seperate between the two actions when it is used on a line with an assignment operator (in both examples, this is the += operation). Lastly I use parentheses mostly to ensure that the two different operations (+= and the ternary expression clause) do not influence or conflict with each other (because in bluemagica's example I could clearly see that the += seems to be A PART OF the expression clause of the ternary operation. That being said, what I mean is that it looks like the clause is not simply "is the MC's x value greater than newX" but rather "is the MC's x value + the MC's x value greater than newX" and it just seems confusing.)
In conclusion, I recommend using ternary operations anywhere that you can easily place them because for larger loops they will likely make a noticable difference. Just be careful when nesting them; the single-line syntax can get pretty confusing/hectic in a hurry:
Code:
// code snippit from a quadruple-nested ternary in a app I wrote
var index:int = ( py > ny ? ( afy > nfy ? getAftNewY() : getAftOldY() ) : ( dy > ody ? ( ady > aody ? getAftOldY() : getAftNewY() ) : getRandomY() );
Last edited by ImprisonedPride; 10-16-2010 at 05:32 PM.
The 'Boose':
ASUS Sabertooth P67 TUF
Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
8GB G.Skill Ripjaws 1600 DDR3
ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
New addition: OCZ Vertex 240GB SATA III SSD
WEI Score: 7.6
-
~ is the bitwise NOT
so ~speed-1 is a high tech way of writing -speed
although Im pretty sure it should be ~speed+1... Ill check it out later
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|