A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: [RESOLVED] rotate to dynamic point

  1. #1
    |-'|-'|
    Join Date
    Jan 2006
    Posts
    273

    resolved [RESOLVED] rotate to dynamic point

    i made this thing that would move to mouse.
    Code:
    -removed. check next post-
    it should rotate to mouse gradually, but i can't seem to get it to rotate fully. it would go the other way most of the time. when the point is directly behind (180deg) it would be confused and jitter left and right. still debuging the math, but it's not my forte. any help is appriceated. thanks.

    p.s. you just need to copy paste the code to test it.
    Last edited by Gu35s; 04-29-2009 at 04:38 AM.

  2. #2
    |-'|-'|
    Join Date
    Jan 2006
    Posts
    273
    got it working. work arounds. here is for those who enjoy watching plane shooting ufo. best in 30 fps and black background.
    Code:
    Stage.scaleMode = 'noScale';
    Stage.align = 'TL';
    speed = 20;
    maxspeed = 15;
    minspeed = 5;
    this.createEmptyMovieClip('flier_mc',2);
    flier_mc.lineStyle(2,0x0000ff,100);
    flier_mc.beginFill(0x00ffff,100);
    flier_mc.moveTo(-15,15);
    flier_mc.lineTo(-3,-5);
    flier_mc.lineTo(0,-15);
    flier_mc.lineTo(3,-5);
    flier_mc.lineTo(15,15);
    flier_mc.lineTo(0,10);
    flier_mc.lineTo(-15,15);
    flier_mc.endFill();
    flier_mc._x = random(Stage.width);
    flier_mc._y = random(Stage.height);
    flier_mc.onEnterFrame = function() {
    	this.point = {x:target_mc._x, y:target_mc._y};
    	this.globalToLocal(this.point);
    	this.disx = -this.point.x;
    	this.disy = -this.point.y;
    	this.ang = -Math.atan2(this.disx, this.disy) / (Math.PI / 180);
    	if (Math.abs(this.ang) < 45) {
    		this._rotation += (this.ang / 4);
    		if (Math.abs(this.ang) < 15) {
    			laser();
    		}
    		speed = (speed < maxspeed) ? speed + 1 : maxspeed;
    	} else {
    		speed = (speed > minspeed) ? speed - .25 : minspeed;
    	}
    	this._rotation += (this.ang / (speed * 2));
    	trail();
    	this.rot2rad = this._rotation * (Math.PI / 180);
    	this._x += (Math.sin(this.rot2rad) * speed);
    	this._y -= (Math.cos(this.rot2rad) * speed);
    	movetrail();
    };
    var t:Number = 0;
    this.createEmptyMovieClip('trail_mc',1);
    function trail() {
    	trailed = trail_mc.createEmptyMovieClip('trail' + t, t);
    	trailed.lineStyle(5,0xff6600,100);
    	trailed.moveTo(flier_mc._x,flier_mc._y);
    }
    function movetrail() {
    	trailed.lineTo(flier_mc._x,flier_mc._y);
    	trailed.onEnterFrame = function() {
    		this._alpha -= 25;
    		if (this._alpha < 0) {
    			this.removeMovieClip();
    		}
    	};
    	t++;
    }
    this.createEmptyMovieClip('target_mc',0);
    target_mc.lineStyle(2,0xff00ff,100);
    target_mc.beginFill(0x660066,100);
    target_mc.moveTo(-10,0);
    target_mc.lineTo(-3,-3);
    target_mc.lineTo(0,-10);
    target_mc.lineTo(3,-3);
    target_mc.lineTo(10,0);
    target_mc.lineTo(3,3);
    target_mc.lineTo(0,10);
    target_mc.lineTo(-3,3);
    target_mc.moveTo(-10,0);
    target_mc.endFill();
    target_mc._x = target_mc.tX = 50;
    target_mc._y = target_mc.tY = 50;
    target_mc.onEnterFrame = function() {
    	this._rotation += 25;
    	if (this._x < (this.tX + 5) and this._x > (this.tX - 5)) {
    		this.tX = 50 + Math.random() * (Stage.width - 50);
    	}
    	if (this._y < (this.tY + 5) and this._y > (this.tY - 5)) {
    		this.tY = 50 + Math.random() * (Stage.height - 50);
    	}
    	this._x += ((this.tX - this._x) / 20);
    	this._y += ((this.tY - this._y) / 20);
    	this._xscale = this._yscale = (this._yscale > 100) ? this._yscale - 25 : 100;
    };
    var las:Number = 0;
    function laser() {
    	lsr = this.createEmptyMovieClip('laser' + las, 1000 + las);
    	lsr.lineStyle(3,0x00ffff,50);
    	lsr.moveTo(0,0);
    	lsr.lineTo(0,-10);
    	lsr.lineStyle(1,0x00ffff,100);
    	lsr.moveTo(0,-1);
    	lsr.lineTo(0,-9);
    	lsr._x = flier_mc._x + (5 - Math.random() * 10);
    	lsr._y = flier_mc._y + (5 - Math.random() * 10);
    	lsr._rotation = flier_mc._rotation + (5 - Math.random() * 10);
    	lsr.onEnterFrame = function() {
    		this.rot2rad = this._rotation * (Math.PI / 180);
    		this._x += (Math.sin(this.rot2rad) * 25);
    		this._y -= (Math.cos(this.rot2rad) * 25);
    		if (this.hitTest(target_mc)) {
    			target_mc._xscale = target_mc._yscale = 150;
    			this.removeMovieClip();
    		}
    		if (this._x < -50 or this._x > Stage.width + 50 or this._y < -50 or this._y > Stage.height + 50) {
    			this.removeMovieClip();
    		}
    	};
    	las++;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center