View Full Version : How to set area boundary in catch game?
fionify
January 22nd, 2007, 06:21 AM
Hi there,
I have a catch game tutorial but the problem is that the catcher which is controlled by arrow keys on the keyboard slides out of the screen. How do I write the code in such a way that it will slide within a boundary?
SacrificialLamb
January 22nd, 2007, 02:26 PM
for horizontal sliding
if (catcher ._x > rightBoundary){
catcher ._x = rightBoundary
} else if (catcher ._x < leftBoundary){
catcher ._x = leftBoundary
}
There might be a better way to do it if I could see the code but this will work
fionify
January 22nd, 2007, 11:31 PM
Here's the file which I found it on the internet.
I wonder if it will require an entirely different programming to control the slider by mouse?
I have inserted the code but it doesnt work, where should I place the code?
SacrificialLamb
January 22nd, 2007, 11:41 PM
when you say "here's the file" you still have to add the link.
The code will not work unless you edit it a bit, rightBoundary and leftBoundary are both variables and you need to set them to some thing and i don't know if the MC name for your catcher is "catcher" and you might need the change that, also if you are adding the code to the catcher MC you could use this.
fionify
January 23rd, 2007, 12:00 AM
when you say "here's the file" you still have to add the link.
The code will not work unless you edit it a bit, rightBoundary and leftBoundary are both variables and you need to set them to some thing and i don't know if the MC name for your catcher is "catcher" and you might need the change that, also if you are adding the code to the catcher MC you could use this.
Sorry just now did not attach the file correctly. For this, the movie clips doesn't have any instance names.
SacrificialLamb
January 23rd, 2007, 12:20 AM
Will the movie clips dose have an instance names and it is "slider_mc". What you need to do is find the code header "slider_mc.onEnterFrame = function() {" and make it look like this
slider_mc.onEnterFrame = function() {
this._x += xSpeed;
if (this._x>(Stage.width-(this._width/2))) {
this._x = (Stage.width-(this._width/2));
xSpeed=0
} else if (this._x<(this._width/2)) {
this._x = (this._width/2);
xSpeed=0
}
};
fionify
January 23rd, 2007, 01:30 AM
when you say "here's the file" you still have to add the link.
The code will not work unless you edit it a bit, rightBoundary and leftBoundary are both variables and you need to set them to some thing and i don't know if the MC name for your catcher is "catcher" and you might need the change that, also if you are adding the code to the catcher MC you could use this.
Will the movie clips dose have an instance names and it is "slider_mc". What you need to do is find the code header "slider_mc.onEnterFrame = function() {" and make it look like this
ActionScript Code:
slider_mc.onEnterFrame = function() {
this._x += xSpeed;
if (this._x>(Stage.width-(this._width/2))) {
this._x = (Stage.width-(this._width/2));
xSpeed=0
} else if (this._x<(this._width/2)) {
this._x = (this._width/2);
xSpeed=0
}
};
Thanks, it works.
Another thing is that if you could notice, the slider_mc seems to be very slow in response. Meaning, It seems to move slowly on initial pressing of arrow key.
How can I make the speed consistent?
SacrificialLamb
January 23rd, 2007, 03:01 AM
It dose that because the arrow key dose not make it move directly but edit the speed.
Will this code increases the speed (note: moving left is a negative speed).
if (Key.getCode() == Key.LEFT) {
if(xSpeed>-maxSpeed){
xSpeed--;
}
}
if (Key.getCode() == Key.RIGHT) {
if(xSpeed<maxSpeed){
xSpeed++;
}
}
So if you change "xSpeed++;" to "xSpeed = 15;" (-15 for left) that will make the speed 15 on the key press and the slider will start moving right away. 15 is about one slider's width ease() running
Or you could change "xSpeed++;" to"xSpeed+=2;" this will make the speed increase 2 times as fast ("++" = var + 1).
You might also want to have a look at how it works with out the ease() function , that is what makes it slow done and not just stop dead.
fionify
January 24th, 2007, 05:49 AM
Okay I increase the ease to "40" and it prevented the slider from having a slow response. Also, I change it to xSpeed = 15 for left and right movements. Thanks for the help and tips.
I hope I am not asking too much in one topic.
I want to have a "bonus ball" which will score bonus points. The bonus ball will appear once in a while (maybe after 45 seconds) and will have to move faster that the normal balls, does this require another set of programming or can I set it somewhere in the code?
It dose that because the arrow key dose not make it move directly but edit the speed.
Will this code increases the speed (note: moving left is a negative speed).
ActionScript Code:
if (Key.getCode() == Key.LEFT) {
if(xSpeed>-maxSpeed){
xSpeed--;
}
}
if (Key.getCode() == Key.RIGHT) {
if(xSpeed<maxSpeed){
xSpeed++;
}
}
So if you change "xSpeed++;" to "xSpeed = 15;" (-15 for left) that will make the speed 15 on the key press and the slider will start moving right away. 15 is about one slider's width ease() running
Or you could change "xSpeed++;" to"xSpeed+=2;" this will make the speed increase 2 times as fast ("++" = var + 1).
You might also want to have a look at how it works with out the ease() function , that is what makes it slow done and not just stop dead.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.