Getting Actionscript to work from the timeline

I'm having issues getting my script to work on the timeline

Page 1 of 1

1 Replies - 1164 Views - Last Post: 15 March 2007 - 03:20 PM

#1 michelle@urban  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 1
  • View blog
  • Posts: 309
  • Joined: 09-July 05

Getting Actionscript to work from the timeline

Posted 15 March 2007 - 01:31 PM

The following is the code that doesn't work, I have got a working swf with a similar code to this that is attached to the mc, this works fine. However, I want to put the script on the frame instead, I've tried adapting it but it isn't working. It's probably something really simple but I can't see it. here's the url to the working swf so that you can see what I'm trying to do; http://www.brainfluf...%20elements.swf

//create variables
	gravity = .3;
	ceil = 0; 
	floor = 500; 
	leftwall = 100;
	rightwall = 700;
	bounce = (40+random(35))/100; 
	speedx = 20-random(40);		
	speedy = 10-random(30);	
	friction = .9;	//against sides of window
	this.redSquare_mc._xscale = _yscale=50-Math.random()*50-100;

this.redSquare_mc.onEnterFrame = function() {
	if (this.redSquare_mc._name == "redSquare_mc") { 
		this.redSquare_mc._x = _root._xmouse;	
		this.redSquare_mc._y = _root._ymouse;	

		i++;
		this.redSquare_mc._visible = false;	
		this.redSquare_mc.onmousedown = function() {	
			this.redSquare_mc.i = 0;		
			this.redSquare_mc._visible = false;	
		};
		while (this.redSquare_mc.i<10) {	
			this.redSquare_mc.duplicateMovieClip("redSquare_mc"+this.redSquare_mc.i, this.redSquare_mc.i + 50);	
			trace(this.i)	
			this.redSquare_mc.i++;
		}
		
		} else {
		
		speedy = speedy+gravity;	
		this.redSquare_mc._x += speedx;		
		this.redSquare_mc._y += speedy;
		
		if (this.redSquare_mc._y+(this.redSquare_mc._height/2)>floor) {
			this.redSquare_mc._y = floor-(this.redSquare_mc._height/2);
			speedy *= -bounce;	
		}
		
		if (this.redSquare_mc._y-(this.redSquare_mc._height/2)<ceil) {
			this.redSquare_mc._y = ceil+(this.redSquare_mc._height/2);
			speedy *= -bounce;	
		}
		
		if (this.redSquare_mc._x-(this.redSquare_mc._width/2)<leftwall) {
			this.redSquare_mc._x = leftwall+(this.redSquare_mc._width/2);
			speedx *= -bounce;	
		}
		
		if (this.redSquare_mc._x+(this.redSquare_mc._width/2)>rightwall) {
			this.redSquare_mc._x = rightwall-(this.redSquare_mc._width/2);
			speedx *= -bounce;	
		}
		
		if (this.redSquare_mc._x+(this.redSquare_mc._width/2)<rightwall && this.redSquare_mc._x-(this.redSquare_mc._width/2)>leftwall && this.redSquare_mc._y == floor-(this.redSquare_mc._height/2)) {
			if (bounce<0) {
				speedx *= -friction;
			} else {
				speedx *= friction;
			}
			if (speedx<.25 && speedx>-.25) {
				speedx = 0;
			}
		}
	}
}

this.redSquare_mc.onRollOver = function(){
	this.redSquare_mc._y -= random(100);	
}


Thanx

This post has been edited by michelle@urban: 15 March 2007 - 01:54 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Getting Actionscript to work from the timeline

#2 theRemix  Icon User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 440
  • Joined: 19-October 05

Re: Getting Actionscript to work from the timeline

Posted 15 March 2007 - 03:20 PM

you are right, this looks like it belongs on a movie clip. and it also looks like you tried to modify it to fit in a frame.

assuming you want this on the first frame of the _root movie clip, and redSquare_mc is the instance name of a movie clip that appears throughout the entire timeline...
try this out:
//create variables
	gravity = .3;
	ceil = 0; 
	floor = 500; 
	leftwall = 100;
	rightwall = 700;
	bounce = (40+random(35))/100; 
	speedx = 20-random(40);		
	speedy = 10-random(30);	
	friction = .9;	//against sides of window
	_root.redSquare_mc._xscale = _root.redSquare_mc._yscale=50-Math.random()*50-100;

_root.redSquare_mc.onEnterFrame = function() {
	if (_root.redSquare_mc._name == "redSquare_mc") { 
		_root.redSquare_mc._x = _root._xmouse;	
		_root.redSquare_mc._y = _root._ymouse;

		i++;
		_root._visible = false;	
		_root.onmousedown = function() {	
			_root.redSquare_mc.i = 0;		
			_root.redSquare_mc._visible = false;	
		};
		while (_root.redSquare_mc.i<10) {	
			_root.redSquare_mc.duplicateMovieClip("redSquare_mc"+_root.redSquare_mc.i, _root.redSquare_mc.i + 50);	
			trace(_root.redSquare_mc.i)	
			_root.redSquare_mc.i++;
		}
		
		} else {
		
		speedy = speedy+gravity;	
		_root.redSquare_mc._x += speedx;		
		_root.redSquare_mc._y += speedy;
		
		if (_root.redSquare_mc._y+(_root.redSquare_mc._height/2)>floor) {
			_root.redSquare_mc._y = floor-(_root.redSquare_mc._height/2);
			speedy *= -bounce;	
		}
		
		if (_root.redSquare_mc._y-(_root.redSquare_mc._height/2)<ceil) {
			_root.redSquare_mc._y = ceil+(_root.redSquare_mc._height/2);
			speedy *= -bounce;	
		}
		
		if (_root.redSquare_mc._x-(_root.redSquare_mc._width/2)<leftwall) {
			_root.redSquare_mc._x = leftwall+(_root.redSquare_mc._width/2);
			speedx *= -bounce;	
		}
		
		if (_root.redSquare_mc._x+(_root.redSquare_mc._width/2)>rightwall) {
			_root.redSquare_mc._x = rightwall-(_root.redSquare_mc._width/2);
			speedx *= -bounce;	
		}
		
		if (_root.redSquare_mc._x+(_root.redSquare_mc._width/2)<rightwall && _root.redSquare_mc._x-(_root.redSquare_mc._width/2)>leftwall && _root.redSquare_mc._y == floor-(_root.redSquare_mc._height/2)) {
			if (bounce<0) {
				speedx *= -friction;
			} else {
				speedx *= friction;
			}
			if (speedx<.25 && speedx>-.25) {
				speedx = 0;
			}
		}
	}
}

_root.redSquare_mc.onRollOver = function(){
	_root.redSquare_mc._y -= random(100);	
}



then read some AS2 books/tutorials




give the above code a try, i haven't checked it, it probably has multiple errors, it looks like you should have this code attached to a movie clip, otherwise instead of using mc.onEnterFrame you can have your _root timeline consist of 2 frames, with this code one one frame, then you won't need onEnterFrame, it will just run everytime it plays that frame (every other frame) set your fps to 30.
this should at least give you a few errors to help you figure out how to get iit to work.


hth a little
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1