Solved

[AS2] scroller help

Posted on 2011-03-10
13
366 Views
Last Modified: 2012-05-11
hello
i am going through a tutroial for a flash platform game and would like to only scroll until the player reaches the stage width. at the moment the scroll is continuous. i have posted my code and would be grateful if someone could tell me how to do it based on the attached code. many thanks
if (dir == "right" && !_root.leftblock.hitTest(this._x+20, this._y, true))
		{
			_root.health._x += speed;
			// moves the health, the opposite way to the _root
			_root.score._x += speed;
			// moves the score, the opposite way to the _root
			this._x += speed;
			// moves the char, the opposite way to the _root
			_root._x -= speed;
			// moves the _root
		}

Open in new window

0
Comment
Question by:peter_coop
  • 7
  • 6
13 Comments
 
LVL 17

Expert Comment

by:deepanjandas
ID: 35095836
Here you go (guessing it to be AS1.0 or AS2.0):
var stageWidth = 550;
if (dir == "right" && !_root.leftblock.hitTest(this._x+20, this._y, true)){

if( this._x < (stageWidth - _width)){
                        _root.health._x += speed;
                        // moves the health, the opposite way to the _root
                        _root.score._x += speed;
                        // moves the score, the opposite way to the _root
                        this._x += speed;
                        // moves the char, the opposite way to the _root
                        _root._x -= speed;
                        // moves the _root
                }
}

Open in new window


Warm Regards
Deepanjan Das
0
 

Author Comment

by:peter_coop
ID: 35097747
hi. still scrolling past the stage. i have posted the whole code for the player with your amendments. thanks
onClipEvent (load) {
	var stageWidth = 768;
	jumping = false;
	// jumping is true
	speed = 0;
	// speed is 0
	//////////////////////
	////Ammendment 1//
	//////////////////
	healthX = _root.health._x;
	//sets healthX to the starting X postition of the "health" MC
	scoreX = _root.score._x;
	//sets scoreX to the starting X postition of the "score" MC
	Xpos = this._x;
	//sets Xpos to the starting X postition of this MC
	Ypos = this._y;
	//sets Ypos to the starting Y postition of this MC
	////////////////
	///////
	maxmove = 15;
	// maxmove is fifteen(max run seed)
	_root.maxshoottime = 100;
	// _root.maxshoottime is set to 100
	// this is how far you want the bullets to travel before deleting
}
onClipEvent (enterFrame) {
	//////////////////////
	////Ammendment 2//
	//////////////////
	_x = Xpos-_root._x;
	//sets the X pos to the starting X postition of this MC
	_root.score._x = scoreX-_root._x;
	//sets the scire MCs X pos to its starting point on the screen
	_root.health._x = healthX-_root._x;
	//sets the health MCs X pos to its starting point on the screen
	if (!_root.ground.hitTest(this._x, this._y, true) && !jumping)
	{
		// if NOT hitting X and Y postion with the ground and NOT jumping
		this._y += 6;
		// Y positon moves up 6
	}
	////////////////     
	///////
	if (!_root.shooting)
	{
		// if _root.shooting is false
		_root.timer = 0;
		// _root.timer is set to 0
		_root.mvsp = _xscale/20;
		// _root.mvsp is set to the chars xscale divided by 20
		// the answer to this is the speed of the bullets
	}
	if (_root.dead)
	{
		// if dead is true
		this.gotoAndStop("dead");
		// goto and stop on the "dead" frame
	} else
	{
		// otherwise (if they are not dead)
		speed *= .85;
		// speed is multiplied by .85
		// the lower the faster is slows
		if (dir == "right" && !_root.leftblock.hitTest(this._x+20, this._y, true))
		{
			_root.health._x += speed;
			// moves the health, the opposite way to the _root
			_root.score._x += speed;
			// moves the score, the opposite way to the _root
			this._x += speed;
			// moves the char, the opposite way to the _root
			_root._x -= speed;
			// moves the _root
		}
		//////////////////////       
		////Ammendment 3//
		//////////////////
		if (speed>0)
		{
			//if speed is smaller than 0
			dir = "right";
			// the variable dir is set to right
		} else if (speed<0)
		{
			//if speed is greater than 0
			dir = "left";
			// the var dir is set to left
		}
		if (dir == "left" && !_root.rightblock.hitTest(this._x-20, this._y, true))
		{
			if (this._x<(stageWidth-_width))
			{
				_root.health._x += speed;
				// moves the health, the opposite way to the _root
				_root.score._x += speed;
				// moves the score, the opposite way to the _root
				this._x += speed;
				// moves the char, the opposite way to the _root
				_root._x -= speed;
				// moves the _root
			}
		}
		if (Key.isDown(Key.LEFT))
		{
			// if left is pressed
			if (speed>-maxmove)
			{
				// if the speed is greater than neg. maxmove
				speed--;
				// speed goes lower
			}
			this.gotoAndStop("run");
			// goto and stop the run frame
			this._xscale = -100;
			// scale is set to neg. 100
			// this is what rotates ur char
			/////////////////// 
			///////////////
		} else if (Key.isDown(Key.RIGHT))
		{
			// otherwise if right is pressed
			if (speed<maxmove)
			{
				// if the speed is smaller than maxmove
				speed++;
				// speed goes up
			}
			this._xscale = 100;
			// scale is set to  100
			// this is what rotates ur char
			this.gotoAndStop("run");
			// goto and stop the run frame
		} else if (Key.isDown(Key.CONTROL))
		{
			// otherwise if control is pressed
			this.gotoAndStop("attack");
			// goto and stop the attack frame
			attacking = true;
			// attacking is true
			speed = 0;
			// speed is set to 0
		} else if (Key.isDown(Key.SPACE))
		{
			// otherwise if space is pressed
			if (_root.gotgun == true && !_root.shooting)
			{
				// if _root.gotgun is true(they have the gun) and _root.shooting is false
				_root.attachMovie("bullet", "bulleter", 1, {_x:_root.char._x, _y:_root.char._y-25});
				// attach the movie with the Linkage name "bullet" to the _root at the character X position and the Y position minus 25
				_root.shooting = true;
				// _root.shooting is set true
				with (_root.bulleter)
				{
					// all code below this code and it's closer refer to _root.bulleter
					onEnterFrame = function () {
						// setting the onEnterFrame events (onClipEvent)
						if (_root.timer>_root.maxshoottime)
						{
							// if _root.timer is smaller than _root.maxshoottime
							_root.shooting = false;
							// shooting is false
							unloadMovie(this);
							// this movie clip is unloaded
						}
						_root.timer++;
						// _root.timer goes up 1
						_x += _root.mvsp;
						// the X goes up _root.mvsp (which is set constantly and stays the same when shooting.)                    };
					};
				}
				attacking = true;
				// attacking is true
				speed = 0;
				// speed is set to 0
				this.gotoAndStop("shoot");
				// goto and stop on the shoot frame
			}
		} else if (speed<1 && speed>-1 && !attacking)
		{
			// if speed is smaller than one and greater than neg. 1
			speed = 0;
			// speed is set to 0
			this.gotoAndStop("idle");
			// gotoAndStop the idle frame
		}
		if (Key.isDown(Key.UP) && !jumping)
		{
			// if up is pressed and NOT jumping
			jumping = true;
			// jumping is set true
		}
		if (jumping)
		{
			// if jumping is true
			this.gotoAndStop("jump");
			this._y -= jump;
			// Y position is set down jump
			jump -= .5;
			// jump is set down .5
			if (jump<0)
			{
				// if jump is smaller than 0
				falling = true;
				// falling is true
			}
			if (jump<-10)
			{
				// if jump is smaller than neg. 5
				jump = -10;
				// jump is set to neg 5
				// capping fall speeds prevents falling through grounds
			}
		}
		if (_root.ground.hitTest(this._x, this._y, true) && falling)
		{
			// if hitting X an Y postions with the ground and falling
			jump = 10;
			// jump is set to 9
			jumping = false;
			// jumping is false
			falling = false;
			// falling is false
		}
	}
}
onClipEvent (keyUp) {
	// on Key Up
	if (Key.getCode() == Key.CONTROL)
	{
		// if the release is control
		attacking = false;
		// attacking is false
	}
}

Open in new window

0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 35103875
trace the 2 variables and let me know what it gives:
this._x
stageWidth

Warm Regards
Deepanjan Das
0
 

Author Comment

by:peter_coop
ID: 35107455
trace(this._x)

277.25
276.4
274.85
272.7
270
266.85
263.35
259.5
255.4
251.05
246.5
241.8
236.95
232
226.95
221.8
216.6
211.3
205.95
200.55
195.1
189.7
185.05
181.1
177.75
174.85
172.5
170.45
168.7
167.25
166
164.95
164.05
164.05
164.05
164.05
164.05
164.05
164.05
164.05
164.05
164.05
164.05
164.05
164.05
164.05
164.05
164.05
164.05
164.05
164.05
164.05
164.05
164.05
164.05
164.05
164.05
164.05
164.05
164.05
163.2
161.6
159.5
156.8
153.6
150.1
146.25
142.2
137.85
133.25
128.6
123.75
118.75
114.6
111
108
105.35
103.2
101.35
99.75
98.4
97.25
96.35
96.35
95.5
93.9
91.75
89.1
85.9
82.4
78.6
74.5
71
68
65.5
63.35
61.6
60
58.75
57.6
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
56.65
55.85
54.25
52.1
49.4
46.25
42.75
38.9
34.8
30.5
25.9
21.25
17.25
13.8
11
8.55
6.5
4.75
3.25
2
0.9
0.05
0.05
0.05
0.05
0.05
0.05
0.05
-0.8
-2.35
-4.5
-7.15
-10.35
-13.85
-17.65
-21.8
-26.15
-29.85
-32.95
-35.6
-37.85
-39.75
-41.35
-42.7
-43.85
-44.85
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7
-45.7

stageWidth:
768

thanks
0
 
LVL 17

Accepted Solution

by:
deepanjandas earned 250 total points
ID: 35108255
Okie so you have to restrict both ways like this:

 if (this._x<(stageWidth-_width) && this._x>0)


Warm Regards
Deepanjan Das
0
 

Author Comment

by:peter_coop
ID: 35111106
thank you. why does this not work for 'right' scroll?

if (this._x>(stageWidth+_width) && this._x<768)

thanks
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 17

Expert Comment

by:deepanjandas
ID: 35116725
Because stageWidth is also 768 I guess.

Warm Regards
Deepanjan Das
0
 

Author Comment

by:peter_coop
ID: 35116918
how would i code for the right? thanks
0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 35119604
what do you mean by right - moving to the right?

Warm Regards
Deepanjan Das
0
 

Author Comment

by:peter_coop
ID: 35121181
the code you posted works for the left direction. when the player hits the left side it stops:

if (this._x<(stageWidth-_width) && this._x>0)

but what i am having trouble with is if the player moves right then he falls off. thanks
0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 35121424
for the right this will also work.
if (this._x<(stageWidth-_width) && this._x>0)


Warm Regards
Deepanjan Das
0
 

Author Comment

by:peter_coop
ID: 35121440
ok. thank you very much for your help.
0
 

Author Closing Comment

by:peter_coop
ID: 35121442
thank you
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
Viewers will learn key ranges in Sampler to make their sampled instruments sound more realistic Gather samples of various notes and drag them to Key Range panel: Set proper root key for each sample: Select all the samples with Command-A (or Ctrl…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now