Hello Luca, LC> Hi to all, how do I simulate a rope in ActionScript?
- try this: /** * author Ivan Dembicki,<BR/> * version 1.0<BR/> * Availability: ActionScript 1.0; Flash Player 6<BR/> * usage example below the code */ class org.dembicki.ElasticCord { // public variables /** * gravity value */ public var gravity:Number = 10; /** * elasticity value */ public var elasticity:Number = .2; /** * friction value */ public var friction:Number = .2; // private variables private var __start_point:Object; private var __end_point:Object; private var __segments:Number = 0; // CONSTRUCTOR /** * ElasticCord class constructor * * @param segments Number. Number of segments * @param start_x x position of cord beginning * @param start_y y position of cord beginning * @param end_x x position of cord end * @param end_y y position of cord end */ function ElasticCord(segments:Number, start_x:Number, start_y:Number, end_x:Number, end_y:Number) { this.initCord(segments, start_x, start_y, end_x, end_y); } // PROPERTIES // _segments public function get _segments():Number { return this.__segments; } // PUBLIC // setStartPoint public function setStartPoint(x:Number, y:Number):Void { this.__start_point.x = x || 0; this.__start_point.y = y || 0; } // setEndPoint public function setEndPoint(x:Number, y:Number):Void { this.__end_point.x = x || 0; this.__end_point.y = y || 0; } // reDraw public function reDraw(mc:MovieClip):Void { // -- recalc -- var segments:Number = (this.__segments*2+1); var elasticity:Number = this.elasticity; var friction:Number = 1-this.friction; var gravity:Number = this.gravity/segments; // mc.moveTo(this.__start_point.x, this.__start_point.y); var current_point:Object = this.__start_point.next_point; while (current_point.next_point) { // target is middlepoint between previous and next points var target_x:Number = (current_point.prev_point.x+current_point.next_point.x)/2; var target_y:Number = (current_point.prev_point.y+current_point.next_point.y)/2; // step current_point.x_step += (target_x-current_point.x)*elasticity; current_point.y_step += (target_y-current_point.y)*elasticity+gravity; current_point.x_step *= friction; current_point.y_step *= friction; // move point current_point.x += current_point.x_step; current_point.y += current_point.y_step; // calc next point current_point = current_point.next_point; } // -- redraw -- if (mc) { var control_point:Object = this.__start_point.next_point; while (control_point.next_point) { mc.curveTo(control_point.x, control_point.y, (control_point.next_point.x+control_point.x)/2, (control_point.next_point.y+control_point.y)/2); control_point = control_point.next_point; } mc.lineTo(control_point.x, control_point.y); } } // PRIVATE private function initCord(segments:Number, start_x:Number, start_y:Number, end_x:Number, end_y:Number):Void { if (!segments || segments<1) { segments = 1; } this.__segments = segments; this.__start_point = {x:start_x || 0, y:start_y || 0}; // var x_step:Number = (end_x-start_x)/segments || 0; var y_step:Number = (end_y-start_y)/segments || 0; var current_point:Object = this.__start_point; while ((--segments)>0) { current_point = this.addPoint(current_point, x_step, y_step); } current_point = this.addPoint(current_point, x_step, y_step); this.__end_point = {x:end_x || 0, y:end_y || 0, prev_point:current_point}; current_point.next_point = this.__end_point; _global.ASSetPropFlags(this.__start_point, "next_point", 7); _global.ASSetPropFlags(this.__end_point, "prev_point", 7); } private function addPoint(prev_point:Object, x_step:Number, y_step:Number):Object { var new_point:Object = {y_step:0, x_step:0, prev_point:prev_point, y:prev_point.y+y_step, x:prev_point.x+x_step}; prev_point.next_point = new_point; return new_point; } } /* // --------------------------------------------------- USAGE --------------------------------------------------- // // // Here we create two movieclips that will represent the beginning and the end of the cord. this.onPointPress = function() { // a handler for dragging the start and the end point of the cord this.onMouseMove = function() { this._x = this._parent._xmouse; this._y = this._parent._ymouse; this._parent.updateCord(); }; this.onMouseUp = function() { delete this.onMouseMove; delete this.onMouseUp; }; }; // creating clips and setting handler for dragging this.createEmptyMovieClip("start_mc", 0); this.createEmptyMovieClip("end_mc", 1); this.start_mc.onPress = this.onPointPress; this.end_mc.onPress = this.onPointPress; delete this.onPointPress; // drawing clip shapes this.start_mc.lineStyle(10, 0, 30); this.end_mc.lineStyle(10, 0, 30); this.start_mc.lineTo(1, 0); this.end_mc.lineTo(1, 0); // positioning points randomly this.start_mc._x = Stage.width*Math.random(); this.end_mc._x = Stage.width*Math.random(); this.start_mc._y = Stage.height*Math.random(); this.end_mc._y = Stage.height*Math.random(); // // ELASTIC CORD USAGE import org.dembicki.ElasticCord; // if starting coordinates are omitted in constructor call, // all intermediate points of cord will appear at (0, 0) // this.first_cord = new ElasticCord(8); // if starting coordinates are specified in constructor call, // intermediate points of cord will be distibuted evenly between start and end points this.first_cord = new ElasticCord(8, this.start_mc._x, start_mc._y, this.end_mc._x, this.end_mc._y); // set custom settings this.first_cord.gravity = 35; this.first_cord.elasticity = .4; this.first_cord.friction = .2; // this.second_cord = new ElasticCord(3, this.start_mc._x, start_mc._y, this.end_mc._x, this.end_mc._y); // // updateCord is called when starting or ending point are dragged this.updateCord = function() { this.first_cord.setStartPoint(this.start_mc._x, start_mc._y); this.first_cord.setEndPoint(this.end_mc._x, this.end_mc._y); this.second_cord.setStartPoint(this.start_mc._x, start_mc._y); this.second_cord.setEndPoint(this.end_mc._x, this.end_mc._y); }; this.updateCord(); // recalculating and refreshing cord view this.onEnterFrame = function() { this.clear(); this.lineStyle(0, 0, 100); this.first_cord.reDraw(this); this.lineStyle(0, 0xFF0000, 100); this.second_cord.reDraw(this); }; // */ -- Ivan Dembicki ____________________________________________________________________________ [EMAIL PROTECTED] | | http://www.design.ru _______________________________________________ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com