Is their an event that makes it possible to have code run periodically but not every frame? (i use good old flash 6)
Printable View
Is their an event that makes it possible to have code run periodically but not every frame? (i use good old flash 6)
You can always use counters to skip frames:Quote:
Originally Posted by helpmeplz
if(mycounter>10){
//run code
mycounter=0;
}
mycounter++;
or use getTimer() to run code after certain time:
if(mytime<getTimer()){
//run code
mytime=getTimer()+3000;
}
I like to use the setInterval for those cases, you can choose every however many milliseconds you want it to runQuote:
Originally Posted by helpmeplz
Let's unearth this famous thread !
Anyone know how I could optimize this test ?
if (Math.abs(this._x-this._width/2-this._parent.progression[mc]._x+this._parent.progression[mc]._width/2)<this._parent.progression[mc].colli+this._width/2-3 and Math.abs(this._y-this._height/2-this._parent.progression[mc]._y+this._parent.progression[mc]._height/2)<this._parent.progression[mc].colli+this._height/2-3) {
It runs dozens (hundreds?) of time per frame in my code so it should be optimized as much as possible :D
Thanks in advance ^^
Firstly, nest the second statement within the first(otherwise it will test both, even if the first one fails, so put the second after the first with no and).
Secondly, if you save a variable of Math.abs, that may be faster.
code:
var _mathabs=Math.abs //Define only once, not every time looped through, so outside of loop
if (_mathabs(this._x-this._width/2-this._parent.progression[mc]._x+this._parent.progression[mc]._width/2)<this._parent.progression[mc].colli+this._width/2-3){
if(_mathabs(this._y-this._height/2-this._parent.progression[mc]._y+this._parent.progression[mc]._height/2)<this._parent.progression[mc].colli+this._height/2-3) {
//It is true
//Do stuff
}
}
That should help a bit.
(Also, you could save a reference to this every time in a local variable, though I don't know if that will help).
you could aslo make a refrence to the _parent.progression[mc] so the script doesn't have to keep looking it up. If it were me i'd make all the variable names nice and small. I think that has a slight impact (if any)
code:
var abs = Math.abs
var ob = this._parent.progression[mc]
//properties, thanks unknownGuy
var w = this._width/2
var h = this._height/2
var obw = ob._width/2
var obh = ob._height/2
var colli = ob.colli
//main loop
onEnterFrame = function(){
//our variables
var x = this._x
var y = this._y
//ob's variables
var obx = ob._x
var oby = ob._y
//check
if(abs(x-w-obx+obw)<colli+w-3){
if(abs(y-h-oby+obh)<colli+h-3){
//do stuff
}
}
}
I've never been sure of this, but Mr. Malee, for the obx, obw, oby, and obj, wouldn't it be faster not to create a local variable since it is only used once(since by creating the variable you are using it once + creating a variable)?
whoops, yes it would, however you need to update the x and y positions
Thanks ! I'll nest the if statements and do a reference to this._parent.progression
I can't make a reference to Math.abs...It's not a constant value but an operation >_<
Anyway, many thanks to both of you :)
why cant you reference Math.abs, are you using it as a function or something?
i do it all the time with Math.floor and Math.sqrt
chuck it on frame one of your main timeline as
_global.abs = Math.abs
O_o
I never tried such a thing. Would it be faster?
Yes.
Just 'cause it's a built in method doesn't mean you can't create a short-cut to it ( I think this was covered in the first few pages of this thread ).
It's no different doing something like
var rnd=Math.random;
than
var health=playerSprite.health;
Squize.
I'm sure this was explained earlier in the thread but I didn't see it. And I haven't looked thorugh all of it but here it is. I've noticed in the very beginning of this thread when you guys were talking about if/else statements. Now when you were talking about this I recall something of a ? statement and how it performs. I'm just wondering what this ? statement is or is just anykind of a statement after the if/else?
As far as I understood it works like this
code:
var myVar= Math.round(Math.random())+1;
var isMyVarEqualToTwo = "The Answer is : " + (myVar == 2 ?"true":"false");
Another question, about collision detection in Shumps...it it less slow to use
code:
everybullet.onEnterFrame = function(){
//test every ennemies with a for...in loop
}
OR
code:
onlyOneMovieClip.onEnterFrame=function(){
for(bullets in the game){
for(ennemies in the game){
//Do hitest
}
}
}
It is generally thought(I believe is true, haven't tested) that one onEnterFrame is the best.
Would I take a performance hit for breaking apart an imported bitmap, edit it, and then regrouping it?
The motivation behind the question is that I am trying find a quick way to break up the frames in a sprite sheet without using trace bitmap (which appears to be really slow). When I copy and paste from an external paint program, the transparency is not preserved, so I have to use break apart + lasso tool to remove the background.
I could preserve transparency by cutting each frame in the sprite sheet and saving them individually, but that is way too tedious.
Any help is appreciated, thanks much!
EDIT: I did some testing, and found that using a bitmap as is was about 3% faster than breaking it up.
I created 60 movieclips of either a bitmap or a broken up one and timed their move across the screen from x=0 to x=600.
Anyone seen similar results?
Jee, sounds like my worst nightmare: breaking apart a bitmap then lasso to create an alpha for sprites: Ahhhhhh! get me out of here.
Well no! get yourself a real image editing program and save your stuff with transparency as gif or png24. I'll go for png24 because that gives you a smoth mask and then in Flash you can compress it as a Jpeg --> optimized as never!
If you cant aford photoshop of one of those, here are some free editing softs, which probably will do the job:
http://www.gimp.org
http://picasa.google.com
http://vicman.net/vcwphoto
http://www.cursorarts.com/ca_imffw.html
http://www.eecs.wsu.edu/paint.net
Some tests were done a while back, using a lassoed alpha is actually faster than if the image has transparency. That said, it is an archaic method, and even though it offers minor speed increases, shouldn't be used.Quote:
Originally Posted by drakz
Try using batch processing in photoshop (or use slices to export the images). If you have Flash 8, try using BitmapData to read from the bitmap instead of cutting it up.
breaking an image apart essentially makes it become a vector shape with a bitmap fill. If it then needs less or more performance than an image with alpha area depends on the amount of curves in the vector shape (so the more curves your "custom alpha mask" has,the more performance it will need).
As pred said its an archaic method,painful to do and a giant time waste.
When did this thread get "unstickied"?