A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: getting a sprite to move continuously after a single keypress ...

  1. #1
    Junior Member
    Join Date
    Mar 2003
    Posts
    21

    getting a sprite to move continuously after a single keypress ...

    I was following a tutorial on moving clips with the keypad - I got the character moving around fine, but just using 'if (Key.isDown(Key.RIGHT)' type statements, which means the sprite just moves while the key is down. So I tried to come up with a way to make the sprite move continuously using this code attached to an mc:

    Code:
    onClipEvent (load) {
    	step = 5;
    	movieWith = 300;
    	movieHeight = 300;
    	this.stop();
    	_root.dir = "";
    }
    onClipEvent (keyDown) {
    	if (Key.getCode() == Key.UP) {
    		_root.dir = "up";
    	} else if (Key.getCode() == Key.RIGHT) {
    		_root.dir = "right";
    	} else if (Key.getCode() == Key.DOWN) {
    		_root.dir = "down";
    	} else if (Key.getCode() == Key.LEFT) {
    		_root.dir = "left";
    	}
    }
    onClipEvent (enterFrame) {
    	if (_root.dir="right" and this._x<300) {
    		this.attachMovie("sprite", "bug", 10);
    		this._x = this._x+step;
    		this._rotation = 45;
    		this._xscale = -100;
    	} else if (_root.dir="left" and this._x>0) {
    		this.attachMovie("sprite", "bug", 10);
    		this._x = this._x-step;
    		this._rotation = -45;
    		this._xscale = 100;
    	} else if (_root.dir="up" and this._y>0) {
    		this.attachMovie("sprite", "bug", 10);
    		this._y = this._y-step;
    		this._rotation = 0;
    		this._xscale = 100;
    	} else if (_root.dir="down" and this._y<300) {
    		this.attachMovie("sprite", "bug", 10);
    		this._y = this._y+step;
    		this._rotation = 180;
    		this._xscale = 100;
    	} else {
    		this.attachMovie("sprite", "bug", 10);
    		_root.dir = "";
    	}
    }
    How can I fix it? Am I on the right track? Is there a different appraoch?

  2. #2
    Senior Member
    Join Date
    Apr 2000
    Location
    Northern Ireland
    Posts
    2,146
    Code:
    onClipEvent (load) {
    	step = 5;
    	movieWith = 300;
    	movieHeight = 300;
    	this.stop();
    	_root.dir = "";
                    this.attachMovie("sprite", "bug", 10);
    }
    onClipEvent (keyDown) {
    	if (Key.isDown(Key.UP)) {
    		_root.dir = "up";
    	} else if (Key.isDown(Key.RIGHT)) {
    		_root.dir = "right";
    	} else if (Key.isDown(Key.DOWN)) {
    		_root.dir = "down";
    	} else if (Key.isDown(Key.LEFT)) {
    		_root.dir = "left";
    	}
    }
    onClipEvent (enterFrame) {
    	if (_root.dir eq "right" && this._x < 300) {
    		this._x += step;
    		this._rotation = 45;
    		this._xscale = -100;  //  what s this for??
    	} else if (_root.dir eq "left" && this._x > 0) {
    		this._x -= step;
    		this._rotation = -45;
    		this._xscale = 100;
    	} else if (_root.dir eq "up" && this._y > 0) {
    		this._y -= step;
    		this._rotation = 0;
    		this._xscale = 100;
    	} else if (_root.dir eq "down" && this._y < 300) {
    		this._y += step;
    		this._rotation = 180;
    		this._xscale = 100;
    	} else {
    		_root.dir = "";
    	}
    }
    That should work a little better. Actually, it should work a lot better, since your original code didn't work!
    ----------
    "Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life." TERRY PRATCHETT

  3. #3
    Junior Member
    Join Date
    Mar 2003
    Posts
    21
    wicked! cheers bro - that's better (the -xscale thing is 2 flip sprite horizontal)

  4. #4
    Senior Member
    Join Date
    Apr 2000
    Location
    Northern Ireland
    Posts
    2,146
    Oh right ... neat trick! Glad the code worked out for you ... make sure you click the 'Mark Thread Resolved' link at the bottom of this page
    ----------
    "Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life." TERRY PRATCHETT

  5. #5
    Junior Member
    Join Date
    Mar 2003
    Posts
    21
    ok, will do - could u check out my pacman problem in the games section if u've got time? I'm getting there slowly by myself - sussed out how to make mazes with arrays/strings - can understand how the level up thing works... but ... pleeeez have a look, it's another movement issue. It's a bit of a big ask, but u might enjoy trying to read the spanish comments ... cough

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