Computer Science Canada

max id error with sprites... CONTINUED

Author:  programmer_needs_help [ Thu Jun 08, 2017 4:55 pm ]
Post subject:  max id error with sprites... CONTINUED

So i'm making a sword fighting game in turing and after repeated use of the attack button an error occurs and the game crashes. It's a max ID error i think becuase each time the player attacks the sprite image is replaced. If you have a suggestion can you please write the code? I'm kind of new to sprites and stuff. Thank You Smile

The following is the code snippet:

% Sword Attack
elsif arrowInput (' ') and playerYValue <= ground then
player := Pic.FileNew ("playerBlazeSword.bmp")
player := Pic.Scale (player, 152, 165)
Sprite.ChangePic (playerSprite, player)
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)
delay (20)
View.Update
player := Pic.FileNew ("playerBlaze.bmp")
player := Pic.Scale (player, 152, 165)
Sprite.ChangePic (playerSprite, player)
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)
delay (20)

plsyerBlazeSword.bmp is the character picture with a sword

playerBlaze is a regular picture of the player

Author:  Insectoid [ Thu Jun 08, 2017 5:57 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

Are you sure it's an illegal sprite ID and not an illegal picture ID? You need to use Pic.Free to delete unnecessary images from memory before you can re-assign the variable.

If you aren't using more than a thousand pictures, you might as well load all of them and give them all unique variables at the beginning of the game. Then you only need to use Sprite.ChangePic to swap, no need for Pic.FileNew.

Also, look into Sprite.Animate. it combines all your standard sprite functions into a single line.

Author:  programmer_needs_help [ Thu Jun 08, 2017 6:06 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

Process "playerMovement": Cannot allocate item. Out of id numbers (max 1000)

is the error message

Author:  programmer_needs_help [ Thu Jun 08, 2017 6:08 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

i don't understand what you mean by "You need to use Pic.Free to delete unnecessary images from memory before you can re-assign the variable." Can you show me where to put that? I tried it before.

Author:  programmer_needs_help [ Thu Jun 08, 2017 6:28 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

omg I fixed it thanks so much!!!!! Your advice worked

Author:  Insectoid [ Thu Jun 08, 2017 6:30 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

You are only allowed to create 1000 images in Turing for technical reasons. Any time you call Pic.FileNew or Pic.Scale, a new image is created. If you're doing that in a loop, it doesn't take long to reach 1000. To get around this, you need to use Pic.Free to delete the pictures you're done using. Deleted pictures no longer count towards the 1000 picture limit.

In your example, you'll need an extra variable. We'll call it 'rawImage', because it will hold the unscaled image.

RawImage := Pic.FileNew ("blah.bmp")
Player := Pic.Scale (RawImage)
Pic.Free (RawImage)

Now, before you chance player to a new image later, you need to free it too:

Pic.free (player)

Author:  programmer_needs_help [ Thu Jun 08, 2017 6:30 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

wait never mind its till crashing

I did this:

elsif arrowInput (' ') and playerYValue <= ground then
Sprite.ChangePic (playerSprite, playerAttack)
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)
delay (20)
View.Update
Sprite.ChangePic (playerSprite, player)
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)
delay (20)

Author:  programmer_needs_help [ Thu Jun 08, 2017 6:31 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

ill try what u just said

Author:  Insectoid [ Thu Jun 08, 2017 6:31 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

What is the error message?

Author:  programmer_needs_help [ Thu Jun 08, 2017 6:33 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

Process "playerMovement": Cannot allocate item. Out of id numbers (max 1000)

is the error message

Author:  programmer_needs_help [ Thu Jun 08, 2017 6:34 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

I tried Pic.Free:

Sprite.ChangePic (playerSprite, playerAttack)
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)
delay (20)
View.Update
Pic.Free (player)
Sprite.ChangePic (playerSprite, player)
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)
delay (20)

now it says:
Process "playerMovement": Pic was freed

Author:  programmer_needs_help [ Thu Jun 08, 2017 6:35 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

If you're wondering, all this code is in the playerMovement process

Author:  Insectoid [ Thu Jun 08, 2017 6:38 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

You have to free the image after you are done with it. Freeing the image deletes it from memory, which means you cannot use it anymore. You can't draw it and you can't assign it to a sprite.

So tell me why you are freeing an image immediately before assigning it to a sprite?

Author:  programmer_needs_help [ Thu Jun 08, 2017 6:41 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

Ok I understand. Now here:

elsif arrowInput (' ') and playerYValue <= ground then
Sprite.ChangePic (playerSprite, playerAttack)
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)
delay (20)
View.Update
Sprite.Free (playerAttack)
Sprite.ChangePic (playerSprite, player)
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)
delay (20)

I'm not using the playerAttack image anymore, yet it still crashes.

Author:  programmer_needs_help [ Thu Jun 08, 2017 6:43 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

Same thing happens when I free the pic at the END:

elsif arrowInput (' ') and playerYValue <= ground then
Sprite.ChangePic (playerSprite, playerAttack)
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)
delay (20)
View.Update
Sprite.Free (playerAttack)
Sprite.ChangePic (playerSprite, player)
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)
Pic.Free (playerAttack)
delay (20)

Author:  programmer_needs_help [ Thu Jun 08, 2017 6:48 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

In the above 2 comments, program crashes after first button press

Author:  Insectoid [ Thu Jun 08, 2017 6:49 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

Are you really done with it at the end of the function? What if you call the function again? The second line uses that image after all.

Author:  programmer_needs_help [ Thu Jun 08, 2017 6:52 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

Well, if its in a loop and I need to call it on command of the user, won't I always need it in memory?

Author:  programmer_needs_help [ Thu Jun 08, 2017 7:09 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

How could I get rid of it if the user can use it throughout the whole game?

Author:  Insectoid [ Thu Jun 08, 2017 7:09 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

Yup. So if the problem is that you're loading images in a loop, why don't you move the code that loads it out of the loop? If it's not in a loop, then it only happens once, and if it only happens once, you won't hit 1000 images. If you don't hit 1000 images, you don't need to free them.

Author:  programmer_needs_help [ Thu Jun 08, 2017 7:12 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

what if i declare it in a separate process without a loop, and then fork it?

Author:  programmer_needs_help [ Thu Jun 08, 2017 7:14 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

Because if I do this outside a loop, it'll only work one time. Won't it?

Author:  programmer_needs_help [ Thu Jun 08, 2017 7:21 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

I tried this:

process thing
Sprite.ChangePic (playerSprite, playerAttack)
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)

View.Update
Sprite.ChangePic (playerSprite, player)
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)
end thing

elsif arrowInput (' ') and playerYValue <= ground then
fork thing

It doesn't seem to crash but there's lag! Sad

Author:  programmer_needs_help [ Thu Jun 08, 2017 7:26 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

There's also an "end thing" line, i just forgot to copy it. Should look like this:

process thing
Sprite.ChangePic (playerSprite, playerAttack)
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)

View.Update
Sprite.ChangePic (playerSprite, player)
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)
end thing

elsif arrowInput (' ') and playerYValue <= ground then
fork thing

Author:  Insectoid [ Thu Jun 08, 2017 7:28 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

Uh, the fewer processes you use, the better. Ideally zero. Use procedures instead. It will save you a lot of problems down the road.

Once you load a picture, you can use it as many times as you want. You can draw it ten times, you can have ten different sprites use it, etc.

However, if you declare it inside a procedure or process, it can only be used inside that process, so don't do that.

Author:  programmer_needs_help [ Thu Jun 08, 2017 7:32 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

If i don't use a process or a procedure, how can i reference that code outside of a loop? I'm so confused

Author:  Insectoid [ Thu Jun 08, 2017 7:39 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

Why do you think you need a process or procedure to access a variable?

Author:  programmer_needs_help [ Thu Jun 08, 2017 7:41 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

Well... You said to do this attack sprite/picture changing outside of the loop. The only thing which I know that can do that yet still be able to reference it is through a process or procedure. And i use the process so i can move multiple things on the screen at once.

Author:  Insectoid [ Thu Jun 08, 2017 7:51 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

I said load the picture outside of the loop. That's only two lives: Pic.FileNew and Pic.Scale. attaching it to a sprite happens after the picture is loaded.

Author:  programmer_needs_help [ Thu Jun 08, 2017 7:55 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

So I can attatch it to the sprite in the loop?

Author:  programmer_needs_help [ Thu Jun 08, 2017 7:56 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

elsif arrowInput (' ') and playerYValue <= ground then
Sprite.ChangePic (playerSprite, playerAttack)
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)

View.Update
Sprite.ChangePic (playerSprite, player)
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)

end if

because this crashes

Author:  Insectoid [ Thu Jun 08, 2017 8:04 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

Is it giving the same error message? Because as long as all images have been loaded, that should run fine.

Author:  programmer_needs_help [ Thu Jun 08, 2017 8:07 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

Process "enemyOneAttack": Cannot allocate item. Out of id numbers (max 1000)

Author:  programmer_needs_help [ Thu Jun 08, 2017 8:07 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

Now it's an error for a new process !!!!

Here's the whole code:

% Indicate Screen Size and Background Image
View.Set ("graphics,offscreenonly")
var window : int := Window.Open ("graphics:930;605,nobuttonbar")
cls

% Indicate Variables
var swordQuestLogoImage : int
var forestBackground : int
const CENTREX : int := maxx div 2
const CENTREY : int := maxy div 2
var msSerifFont : int
var startOption : char
var player : int
var playerSprite : int
var playerYValue : int := 290
var ground : int := 290
var playerXValue : int := CENTREX - 400
var playerXValueOriginal : int
var enemyOne : int
var enemyOneSprite : int
var enemyOneYValue : int := ground
var enemyOneXValue : int := CENTREX + 400
var enemyTwo : int
var enemyThree : int
var lordZolton : int
var pressSToStartPic : int
var pressSToStartSprite : int
var arrowInput : array char of boolean
var arrowInputTwo : array char of boolean
var arrowInputThree : array char of boolean
var mainMenu : int
var levelOneBackground : int
var aboutGameButtonPic : int
var controlsButtonPic : int
var startButtonPic : int
var quitGameButtonPic : int
var aboutGameButton : int
var controlsButton : int
var startButton : int
var quitGameButton : int
var aboutGameScreenOne : int
var controlsScreen : int
var fireball : int
var fireballSprite : int
var fireballXValue : int
var enemyOnePlayerDistance : real := 0
var attackChance : int
var numLives : int := 3
var gameOver : int
var lifeHearts : int
var lifeHeartsSprite : int
var playerAttack : int

% Level One

% Import Level Background
levelOneBackground := Pic.FileNew ("levelOneBackground.bmp")
levelOneBackground := Pic.Scale (levelOneBackground, 930, 605)
Pic.Draw (levelOneBackground, 0, 0, picMerge)

% Import Player and Convert To Sprite
player := Pic.FileNew ("playerBlaze.bmp")
player := Pic.Scale (player, 155, 165)
playerSprite := Sprite.New (player)
Sprite.Show (playerSprite)
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)
playerAttack := Pic.FileNew ("playerBlazeSword.bmp")
playerAttack := Pic.Scale (playerAttack, 152, 165)

% Import Enemy One and Convert To Sprite
enemyOne := Pic.FileNew ("enemyOne.bmp")
enemyOne := Pic.Scale (enemyOne, 155, 166)
enemyOneSprite := Sprite.New (enemyOne)
Sprite.Show (enemyOneSprite)
Sprite.SetPosition (enemyOneSprite, 500, 290, true)


% Import Fireball and Convert To Sprite
fireball := Pic.FileNew ("fireball.bmp")
fireball := Pic.Scale (fireball, 40, 30)
fireballSprite := Sprite.New (fireball)

% Import Life Hearts and Convert To Sprite
lifeHearts := Pic.FileNew ("lifeHeartsThree.bmp")
lifeHearts := Pic.Scale (lifeHearts, 120, 40)
lifeHeartsSprite := Sprite.New (lifeHearts)
Sprite.SetPosition (lifeHeartsSprite, CENTREX - 370, CENTREY + 250, true)
Sprite.Show (lifeHeartsSprite)
Sprite.SetPosition (lifeHeartsSprite, CENTREX - 370, CENTREY + 250, true)

% Begin Process For Player Movement
process playerMovement
loop

% Arrow Key Input

% Player Movement With Down Key
Input.KeyDown (arrowInput)

% Player Movement With Up Key

if arrowInput (KEY_UP_ARROW) and playerYValue = ground then
playerXValueOriginal := playerXValue
loop
playerYValue := floor (-0.015 * ((playerXValue - playerXValueOriginal) div 2) ** 2 + 420)
playerXValue := playerXValue + 1
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, false)
Sprite.Show (playerSprite)
delay (2)
exit when playerYValue <= ground
end loop
% Reset Player To Bottom Of Screen When Top of Screen Reached


% Player Movement With Right Key
elsif arrowInput (KEY_RIGHT_ARROW) then
playerXValue := playerXValue + 1

% Restrict Player Movement on Right Side of Screen
if playerXValue >= maxx then
playerXValue := 0
end if

% Player Movement With Left Key
elsif arrowInput (KEY_LEFT_ARROW) then
playerXValue := playerXValue - 1

% Restrict Player Movement on Left Side of Screen
if playerXValue = 0 then
playerXValue := maxx
end if

% Sword Attack
elsif arrowInput (' ') and playerYValue <= ground then
Sprite.ChangePic (playerSprite, playerAttack)
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)

View.Update
Sprite.ChangePic (playerSprite, player)
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)

end if
% Reposition The Player On The Screen According To Input
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)

delay (2)
end loop
end playerMovement


% Fireball Attack
process fireballMoving
loop
Input.KeyDown (arrowInputTwo)
if arrowInputTwo (KEY_DOWN_ARROW) and playerYValue <= ground then
fireballXValue := playerXValue
for fireballMovement2 : 1 .. 940
Sprite.SetPosition (fireballSprite, fireballXValue + fireballMovement2, ground, true)
delay (2)
end for
Sprite.Show (fireballSprite)
end if
end loop
end fireballMoving

% Enemy AI
[color=darkred]process enemyOneAttack
var a : int := 0
loop
enemyOnePlayerDistance := Math.Distance (playerXValue, playerYValue, enemyOneXValue, enemyOneYValue)
if enemyOnePlayerDistance >= 15 then
a := 0
end if
if enemyOnePlayerDistance > 0 and playerXValue < enemyOneXValue then
enemyOneXValue := enemyOneXValue - 2
Sprite.SetPosition (enemyOneSprite, enemyOneXValue, enemyOneYValue, true)
delay (7)
elsif enemyOnePlayerDistance > 0 and playerXValue > enemyOneXValue then
enemyOneXValue := enemyOneXValue + 2
Sprite.SetPosition (enemyOneSprite, enemyOneXValue, enemyOneYValue, true)
elsif enemyOnePlayerDistance < 15 then
randint (attackChance, 1, 4)
if attackChance = 1 then
Sprite.Hide (enemyOneSprite)
enemyOne := Pic.FileNew ("enemyOneSword.bmp")
Sprite.ChangePic (enemyOneSprite, enemyOne)
enemyOne := Pic.Scale (enemyOne, 152, 165)

enemyOneSprite := Sprite.New (enemyOne)
Sprite.Show (enemyOneSprite)
Sprite.SetPosition (enemyOneSprite, enemyOneXValue, enemyOneYValue, true)
delay (20)
View.Update
if a = 0 then
numLives := numLives - 1
if numLives = 2 then
Sprite.Hide (lifeHeartsSprite)
lifeHearts := Pic.FileNew ("lifeHeartsTwo.bmp")
lifeHearts := Pic.Scale (lifeHearts, 80, 40)
Sprite.ChangePic (lifeHeartsSprite, lifeHearts)
Sprite.Show (lifeHeartsSprite)
Sprite.SetPosition (lifeHeartsSprite, CENTREX - 370, CENTREY + 250, true)
elsif numLives = 1 then
Sprite.Hide (lifeHeartsSprite)
lifeHearts := Pic.FileNew ("lifeHeartsOne.bmp")
lifeHearts := Pic.Scale (lifeHearts, 40, 40)
Sprite.ChangePic (lifeHeartsSprite, lifeHearts)
Sprite.Show (lifeHeartsSprite)
Sprite.SetPosition (lifeHeartsSprite, CENTREX - 370, CENTREY + 250, true)
end if
a := 1
end if
exit when numLives = 0
end if
end if
delay (7)
end loop

Sprite.Hide (enemyOneSprite)
Sprite.Hide (playerSprite)
gameOver := Pic.FileNew ("gameOver.bmp")
gameOver := Pic.Scale (gameOver, 930, 605)
Pic.Draw (gameOver, 0, 0, picMerge)
loop
Sprite.Hide (lifeHeartsSprite)
Sprite.Hide (enemyOneSprite)
Sprite.Hide (playerSprite)
Sprite.Hide (fireballSprite)
end loop
end enemyOneAttack

[/color]

fork enemyOneAttack
fork fireballMoving
fork playerMovement

Author:  programmer_needs_help [ Thu Jun 08, 2017 8:10 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

Sorry some of that isn't readable, Here read this:

% Indicate Screen Size and Background Image
View.Set ("graphics,offscreenonly")
var window : int := Window.Open ("graphics:930;605,nobuttonbar")
cls

% Indicate Variables
var swordQuestLogoImage : int
var forestBackground : int
const CENTREX : int := maxx div 2
const CENTREY : int := maxy div 2
var msSerifFont : int
var startOption : char
var player : int
var playerSprite : int
var playerYValue : int := 290
var ground : int := 290
var playerXValue : int := CENTREX - 400
var playerXValueOriginal : int
var enemyOne : int
var enemyOneSprite : int
var enemyOneYValue : int := ground
var enemyOneXValue : int := CENTREX + 400
var enemyTwo : int
var enemyThree : int
var lordZolton : int
var pressSToStartPic : int
var pressSToStartSprite : int
var arrowInput : array char of boolean
var arrowInputTwo : array char of boolean
var arrowInputThree : array char of boolean
var mainMenu : int
var levelOneBackground : int
var aboutGameButtonPic : int
var controlsButtonPic : int
var startButtonPic : int
var quitGameButtonPic : int
var aboutGameButton : int
var controlsButton : int
var startButton : int
var quitGameButton : int
var aboutGameScreenOne : int
var controlsScreen : int
var fireball : int
var fireballSprite : int
var fireballXValue : int
var enemyOnePlayerDistance : real := 0
var attackChance : int
var numLives : int := 3
var gameOver : int
var lifeHearts : int
var lifeHeartsSprite : int
var playerAttack : int

% Level One

% Import Level Background
levelOneBackground := Pic.FileNew ("levelOneBackground.bmp")
levelOneBackground := Pic.Scale (levelOneBackground, 930, 605)
Pic.Draw (levelOneBackground, 0, 0, picMerge)

% Import Player and Convert To Sprite
player := Pic.FileNew ("playerBlaze.bmp")
player := Pic.Scale (player, 155, 165)
playerSprite := Sprite.New (player)
Sprite.Show (playerSprite)
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)
playerAttack := Pic.FileNew ("playerBlazeSword.bmp")
playerAttack := Pic.Scale (playerAttack, 152, 165)

% Import Enemy One and Convert To Sprite
enemyOne := Pic.FileNew ("enemyOne.bmp")
enemyOne := Pic.Scale (enemyOne, 155, 166)
enemyOneSprite := Sprite.New (enemyOne)
Sprite.Show (enemyOneSprite)
Sprite.SetPosition (enemyOneSprite, 500, 290, true)


% Import Fireball and Convert To Sprite
fireball := Pic.FileNew ("fireball.bmp")
fireball := Pic.Scale (fireball, 40, 30)
fireballSprite := Sprite.New (fireball)

% Import Life Hearts and Convert To Sprite
lifeHearts := Pic.FileNew ("lifeHeartsThree.bmp")
lifeHearts := Pic.Scale (lifeHearts, 120, 40)
lifeHeartsSprite := Sprite.New (lifeHearts)
Sprite.SetPosition (lifeHeartsSprite, CENTREX - 370, CENTREY + 250, true)
Sprite.Show (lifeHeartsSprite)
Sprite.SetPosition (lifeHeartsSprite, CENTREX - 370, CENTREY + 250, true)

% Begin Process For Player Movement
process playerMovement
loop

% Arrow Key Input

% Player Movement With Down Key
Input.KeyDown (arrowInput)

% Player Movement With Up Key

if arrowInput (KEY_UP_ARROW) and playerYValue = ground then
playerXValueOriginal := playerXValue
loop
playerYValue := floor (-0.015 * ((playerXValue - playerXValueOriginal) div 2) ** 2 + 420)
playerXValue := playerXValue + 1
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, false)
Sprite.Show (playerSprite)
delay (2)
exit when playerYValue <= ground
end loop
% Reset Player To Bottom Of Screen When Top of Screen Reached


% Player Movement With Right Key
elsif arrowInput (KEY_RIGHT_ARROW) then
playerXValue := playerXValue + 1

% Restrict Player Movement on Right Side of Screen
if playerXValue >= maxx then
playerXValue := 0
end if

% Player Movement With Left Key
elsif arrowInput (KEY_LEFT_ARROW) then
playerXValue := playerXValue - 1

% Restrict Player Movement on Left Side of Screen
if playerXValue = 0 then
playerXValue := maxx
end if

% Sword Attack
elsif arrowInput (' ') and playerYValue <= ground then
Sprite.ChangePic (playerSprite, playerAttack)
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)

View.Update
Sprite.ChangePic (playerSprite, player)
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)

end if
% Reposition The Player On The Screen According To Input
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)

delay (2)
end loop
end playerMovement


% Fireball Attack
process fireballMoving
loop
Input.KeyDown (arrowInputTwo)
if arrowInputTwo (KEY_DOWN_ARROW) and playerYValue <= ground then
fireballXValue := playerXValue
for fireballMovement2 : 1 .. 940
Sprite.SetPosition (fireballSprite, fireballXValue + fireballMovement2, ground, true)
delay (2)
end for
Sprite.Show (fireballSprite)
end if
end loop
end fireballMoving

% Enemy AI
process enemyOneAttack
var a : int := 0
loop
enemyOnePlayerDistance := Math.Distance (playerXValue, playerYValue, enemyOneXValue, enemyOneYValue)
if enemyOnePlayerDistance >= 15 then
a := 0
end if
if enemyOnePlayerDistance > 0 and playerXValue < enemyOneXValue then
enemyOneXValue := enemyOneXValue - 2
Sprite.SetPosition (enemyOneSprite, enemyOneXValue, enemyOneYValue, true)
delay (7)
elsif enemyOnePlayerDistance > 0 and playerXValue > enemyOneXValue then
enemyOneXValue := enemyOneXValue + 2
Sprite.SetPosition (enemyOneSprite, enemyOneXValue, enemyOneYValue, true)
elsif enemyOnePlayerDistance < 15 then
randint (attackChance, 1, 4)
if attackChance = 1 then
Sprite.Hide (enemyOneSprite)
enemyOne := Pic.FileNew ("enemyOneSword.bmp")
Sprite.ChangePic (enemyOneSprite, enemyOne)
enemyOne := Pic.Scale (enemyOne, 152, 165)

enemyOneSprite := Sprite.New (enemyOne)
Sprite.Show (enemyOneSprite)
Sprite.SetPosition (enemyOneSprite, enemyOneXValue, enemyOneYValue, true)
delay (20)
View.Update
if a = 0 then
numLives := numLives - 1
if numLives = 2 then
Sprite.Hide (lifeHeartsSprite)
lifeHearts := Pic.FileNew ("lifeHeartsTwo.bmp")
lifeHearts := Pic.Scale (lifeHearts, 80, 40)
Sprite.ChangePic (lifeHeartsSprite, lifeHearts)
Sprite.Show (lifeHeartsSprite)
Sprite.SetPosition (lifeHeartsSprite, CENTREX - 370, CENTREY + 250, true)
elsif numLives = 1 then
Sprite.Hide (lifeHeartsSprite)
lifeHearts := Pic.FileNew ("lifeHeartsOne.bmp")
lifeHearts := Pic.Scale (lifeHearts, 40, 40)
Sprite.ChangePic (lifeHeartsSprite, lifeHearts)
Sprite.Show (lifeHeartsSprite)
Sprite.SetPosition (lifeHeartsSprite, CENTREX - 370, CENTREY + 250, true)
end if
a := 1
end if
exit when numLives = 0
end if
end if
delay (7)
end loop

Sprite.Hide (enemyOneSprite)
Sprite.Hide (playerSprite)
gameOver := Pic.FileNew ("gameOver.bmp")
gameOver := Pic.Scale (gameOver, 930, 605)
Pic.Draw (gameOver, 0, 0, picMerge)
loop
Sprite.Hide (lifeHeartsSprite)
Sprite.Hide (enemyOneSprite)
Sprite.Hide (playerSprite)
Sprite.Hide (fireballSprite)
end loop
end enemyOneAttack


fork enemyOneAttack
fork fireballMoving
fork playerMovement

Author:  Insectoid [ Thu Jun 08, 2017 8:13 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

The error is happening for the same reason as before. You have made the same mistake.

Author:  programmer_needs_help [ Thu Jun 08, 2017 8:18 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

omg I can't believe it! Alright lol i'll try and fix that.

Author:  programmer_needs_help [ Thu Jun 08, 2017 8:57 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

upon testing, the following doesn't crash:

% Indicate Screen Size and Background Image
View.Set ("graphics,offscreenonly")
var window : int := Window.Open ("graphics:930;605,nobuttonbar")
cls

% Indicate Variables
var swordQuestLogoImage : int
var forestBackground : int
const CENTREX : int := maxx div 2
const CENTREY : int := maxy div 2
var msSerifFont : int
var startOption : char
var player : int
var playerSprite : int
var playerYValue : int := 290
var ground : int := 290
var playerXValue : int := CENTREX - 400
var playerXValueOriginal : int
var enemyOne : int
var enemyOneSprite : int
var enemyOneYValue : int := ground
var enemyOneXValue : int := CENTREX + 400
var enemyTwo : int
var enemyThree : int
var lordZolton : int
var pressSToStartPic : int
var pressSToStartSprite : int
var arrowInput : array char of boolean
var arrowInputTwo : array char of boolean
var arrowInputThree : array char of boolean
var mainMenu : int
var levelOneBackground : int
var aboutGameButtonPic : int
var controlsButtonPic : int
var startButtonPic : int
var quitGameButtonPic : int
var aboutGameButton : int
var controlsButton : int
var startButton : int
var quitGameButton : int
var aboutGameScreenOne : int
var controlsScreen : int
var fireball : int
var fireballSprite : int
var fireballXValue : int
var enemyOnePlayerDistance : real := 0
var attackChance : int
var numLives : int := 3
var gameOver : int
var lifeHearts : int
var lifeHeartsSprite : int
var playerAttack : int
var enemyOneSword : int

% Level One

% Import Level Background
levelOneBackground := Pic.FileNew ("levelOneBackground.bmp")
levelOneBackground := Pic.Scale (levelOneBackground, 930, 605)
Pic.Draw (levelOneBackground, 0, 0, picMerge)

% Import Player and Convert To Sprite
player := Pic.FileNew ("playerBlaze.bmp")
player := Pic.Scale (player, 155, 165)
playerSprite := Sprite.New (player)
Sprite.Show (playerSprite)
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)
playerAttack := Pic.FileNew ("playerBlazeSword.bmp")
playerAttack := Pic.Scale (playerAttack, 152, 165)

% Import Enemy One and Convert To Sprite
enemyOne := Pic.FileNew ("enemyOne.bmp")
enemyOne := Pic.Scale (enemyOne, 155, 166)
enemyOneSprite := Sprite.New (enemyOne)
Sprite.Show (enemyOneSprite)
Sprite.SetPosition (enemyOneSprite, 500, 290, true)
enemyOneSword := Pic.FileNew ("enemyOneSword.bmp")
enemyOneSword := Pic.Scale (enemyOneSword, 152, 165)

% Import Fireball and Convert To Sprite
fireball := Pic.FileNew ("fireball.bmp")
fireball := Pic.Scale (fireball, 40, 30)
fireballSprite := Sprite.New (fireball)

% Import Life Hearts and Convert To Sprite
lifeHearts := Pic.FileNew ("lifeHeartsThree.bmp")
lifeHearts := Pic.Scale (lifeHearts, 120, 40)
lifeHeartsSprite := Sprite.New (lifeHearts)
Sprite.SetPosition (lifeHeartsSprite, CENTREX - 370, CENTREY + 250, true)
Sprite.Show (lifeHeartsSprite)
Sprite.SetPosition (lifeHeartsSprite, CENTREX - 370, CENTREY + 250, true)

% Begin Process For Player Movement
process playerMovement
loop

% Arrow Key Input

% Player Movement With Down Key
Input.KeyDown (arrowInput)

% Player Movement With Up Key

if arrowInput (KEY_UP_ARROW) and playerYValue = ground then
playerXValueOriginal := playerXValue
loop
playerYValue := floor (-0.015 * ((playerXValue - playerXValueOriginal) div 2) ** 2 + 420)
playerXValue := playerXValue + 1
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, false)
Sprite.Show (playerSprite)
delay (2)
exit when playerYValue <= ground
end loop
% Reset Player To Bottom Of Screen When Top of Screen Reached


% Player Movement With Right Key
elsif arrowInput (KEY_RIGHT_ARROW) then
playerXValue := playerXValue + 1

% Restrict Player Movement on Right Side of Screen
if playerXValue >= maxx then
playerXValue := 0
end if

% Player Movement With Left Key
elsif arrowInput (KEY_LEFT_ARROW) then
playerXValue := playerXValue - 1

% Restrict Player Movement on Left Side of Screen
if playerXValue = 0 then
playerXValue := maxx
end if

% Sword Attack
elsif arrowInput (' ') and playerYValue <= ground then
Sprite.ChangePic (playerSprite, playerAttack)
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)

View.Update
Sprite.ChangePic (playerSprite, player)
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)

end if
% Reposition The Player On The Screen According To Input
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)

delay (2)
end loop
end playerMovement


% Fireball Attack
process fireballMoving
loop
Input.KeyDown (arrowInputTwo)
if arrowInputTwo (KEY_DOWN_ARROW) and playerYValue <= ground then
fireballXValue := playerXValue
for fireballMovement2 : 1 .. 940
Sprite.SetPosition (fireballSprite, fireballXValue + fireballMovement2, ground, true)
delay (2)
end for
Sprite.Show (fireballSprite)
end if
end loop
end fireballMoving

% Enemy AI
process enemyOneAttack
var a : int := 0
loop
enemyOnePlayerDistance := Math.Distance (playerXValue, playerYValue, enemyOneXValue, enemyOneYValue)
if enemyOnePlayerDistance >= 15 then
a := 0
end if
if enemyOnePlayerDistance > 0 and playerXValue < enemyOneXValue then
enemyOneXValue := enemyOneXValue - 2
Sprite.SetPosition (enemyOneSprite, enemyOneXValue, enemyOneYValue, true)
delay (7)
elsif enemyOnePlayerDistance > 0 and playerXValue > enemyOneXValue then
enemyOneXValue := enemyOneXValue + 2
Sprite.SetPosition (enemyOneSprite, enemyOneXValue, enemyOneYValue, true)
elsif enemyOnePlayerDistance < 15 then
randint (attackChance, 1, 4)
if attackChance = 1 then
Sprite.ChangePic (enemyOneSprite, enemyOneSword)
Sprite.SetPosition (enemyOneSprite, enemyOneXValue, enemyOneYValue, true)
View.Update

if a = 0 then
numLives := numLives - 1
if numLives = 2 then
Sprite.Hide (lifeHeartsSprite)
lifeHearts := Pic.FileNew ("lifeHeartsTwo.bmp")
lifeHearts := Pic.Scale (lifeHearts, 80, 40)
Sprite.ChangePic (lifeHeartsSprite, lifeHearts)
Sprite.Show (lifeHeartsSprite)
Sprite.SetPosition (lifeHeartsSprite, CENTREX - 370, CENTREY + 250, true)
elsif numLives = 1 then
Sprite.Hide (lifeHeartsSprite)
lifeHearts := Pic.FileNew ("lifeHeartsOne.bmp")
lifeHearts := Pic.Scale (lifeHearts, 40, 40)
Sprite.ChangePic (lifeHeartsSprite, lifeHearts)
Sprite.Show (lifeHeartsSprite)
Sprite.SetPosition (lifeHeartsSprite, CENTREX - 370, CENTREY + 250, true)
end if
a := 1
end if
exit when numLives = 0
end if
end if
delay (7)
end loop

Sprite.Hide (enemyOneSprite)
Sprite.Hide (playerSprite)
gameOver := Pic.FileNew ("gameOver.bmp")
gameOver := Pic.Scale (gameOver, 930, 605)
Pic.Draw (gameOver, 0, 0, picMerge)
loop
Sprite.Hide (lifeHeartsSprite)
Sprite.Hide (enemyOneSprite)
Sprite.Hide (playerSprite)
Sprite.Hide (fireballSprite)
end loop
end enemyOneAttack



fork enemyOneAttack
fork fireballMoving
fork playerMovement


however, when I press and hold the attack button (space bar) the enemy AI movements slow down

Author:  programmer_needs_help [ Thu Jun 08, 2017 8:58 pm ]
Post subject:  RE:max id error with sprites... CONTINUED

sorry for the late message!


: