Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / [SOLVED] Want to spawn a Box to the left of character (X)

Author
Message
prinsallan
2
Years of Service
User Offline
Joined: 29th Jul 2021
Location:
Posted: 29th Jul 2021 07:17
Hi everybody.

Im a beginner with only a few days of coding and I need help with "spawning" a box.
Its just the start of a long line of questions, but how do I do that?
My code is as follows:
//------------------------------------------------------------------------------------------------------
function EditLevel() //Adds object to the left of characters position
texture = LoadImage("skull-white.jpg") // Load image from the media folder called skull-white.jpg and assigned its ID to an integer variable called "texture"

MyBox = CreateObjectBox(1,1,1) //1 equals size in X,Y,Z
MoveObjectLocalX(MyBox, PlayerOne.ID -1)
MoveObjectLocalY(MyBox, PlayerOne.ID)
MoveObjectLocalZ(MyBox, PlayerOne.ID)
RotateObjectGlobalX(MyBox,0) // Rotate the box if you need to
RotateObjectGlobalY(MyBox,0) // Rotate the box if you need to
RotateObjectGlobalZ(MyBox,0) // Rotate the box if you need to
SetObjectImage(MyBox, texture, 0) // Assigns the texture image to MyBox in texture layer 0
sync()

endfunction
//------------------------------------------------------------------------------------------------------
I am calling the funtion in my player movements with this simple line:
if(GetRawKeyPressed(KEY_E))
EditLevel()
endif
//------------------------------------------------------------------------------------------------------
What am I missing here?
Any help appreciated!
-Jonas

The author of this post has marked a post as an answer.

Go to answer

JonEnstrom
3
Years of Service
User Offline
Joined: 27th Apr 2020
Location:
Posted: 29th Jul 2021 12:45 Edited at: 29th Jul 2021 12:47
This post has been marked by the post author as the answer.
You are close! You need to first place the box at the location of the player, and THEN move it "locally" to the "left" which is -X.

First you need to use SetObjectPosition, so something like:

SetObjectPosition(MyBox, GetObjectX(PlayerOne.ID), GetObjectY(PlayerOne.ID), GetObjectZ(PlayerOne.ID))

Then you move it with MoveObjectLocalX, and you actually don't need the next two lines (MoveObjectLocalY and MoveObjectLocalZ).

A couple more things as well, I would recommend that you load the texture somewhere else in the beginning of your code rather than in the function. The image only needs to get loaded one time, but this function will load it every time it's called. You don't need to call Sync() in the function, you typically only want to call Sync once per game loop, typically at the very end of your logic code.
prinsallan
2
Years of Service
User Offline
Joined: 29th Jul 2021
Location:
Posted: 29th Jul 2021 15:14
Thanks Jon- got it working now.

Login to post a reply

Server time is: 2024-04-24 23:40:17
Your offset time is: 2024-04-24 23:40:17