Groups | Blog | Home
all groups > flash actionscript > august 2006 >

flash actionscript : Movement based on 2 variables...


VaporAction
8/26/2006 8:57:07 PM
I wasn't sure how to title this problem...anyway, the scenario is as follows...
The user drags 2 charged particles (ParticlePositive1_mc and
ParticlePositive2_mc) to a ruler...when they come into contact with the ruler
(HitTest_mc), a hitTest is run to determine if they're both on the ruler and
then their location in relation to each other is checked to determine which
direction they are to begin traveling...(it's for an animation demonstrating
like particles attract and opposite particles repel)...
Everything works up until when the movement should begin...here's my code...


ParticlePositive1_mc.onPress = function() {
this.startDrag(false, 213, 50, 574, 294)
}
ParticlePositive1_mc.onRelease = function() {
this.stopDrag()
if ((this.hitTest(HitTest_mc)) && (ParticlePositive2_mc.hitTest(HitTest_mc)))
{
delete this.onPress
delete ParticlePositive2_mc.onPress
if (this._x < ParticlePositive2_mc._x) {
this.onEnterFrame = function() {
this._x = this._x--
ParticlePositive2_mc._x = ParticlePositive2_mc._x++
}
}
else if (this._x > ParticlePositive2_mc._x) {
this.onEnterFrame = function() {
this._x = this._x++
ParticlePositive2_mc._x = ParticlePositive2_mc._x--
}
}
else if (this.hitTest(ParticlePositive2_mc)) {
this._x = this._x
ParticlePositive2_mc._x = ParticlePositive2_mc._x
}
}
}
kglad
8/27/2006 2:05:30 AM
your use of the -- and ++ operators is incorrect and causing your problem.
instead of:



this._x=this._x--; // (which will result in no change of "this" _x property)

// use:

this._x--; // which will decrement "this" _x property on each loop.
VaporAction
8/27/2006 2:54:56 PM
Thanks kglad...that did the job!
Now for another question...
How would I get the particle to pause for like a second before beginning its
movement?
The way it works now it starts moving as soon as it touches the ruler...
kglad
8/27/2006 3:05:33 PM
you can use setInterval() to delay execution of any function. so, if you place
your particle movement in a function can call that function with a delay length
you want.

you'll clear that interval the first time the function is called and then use
another setInterval() to repeatedly execute the particle movement. you'll
clear that 2nd interval when the particles have left the demonstration area or
have collided.
VaporAction
8/28/2006 3:41:45 PM
I tried the setInterval but it isn't calling the function (Move) for some
reason...I put a trace in the function just to make sure...I also know that I
haven't put the 2nd interval in yet to keep calling the function...if I put the
function (Move) outside the ParticlePositive1_mc.onRelease then it works...and
there's no script errors(I know I'm missing some brackets but it's just cause I
didn't show all the code)....



var Times_n:Number = 0
var Interval_n:Number = setInterval(Move, 1000)

ParticlePositive1_mc.onPress = function() {
this.startDrag(false, 213, 50, 574, 294)
}
ParticlePositive1_mc.onRelease = function() {
this.stopDrag()
this.onEnterFrame = function() {
if ((this.hitTest(HitTest_mc)) &&
(ParticlePositive2_mc.hitTest(HitTest_mc))) {
delete this.onPress
delete ParticlePositive2_mc.onPress
if (this._x < ParticlePositive2_mc._x) {
function Move():Void {
trace ("function Move is being called")
this._x--
ParticlePositive2_mc._x++
Times_n++
if (Times_n >=1) {
clearInterval(Interval_n)
}
}
}
kglad
8/29/2006 2:41:19 PM
try:

AddThis Social Bookmark Button