Projectiles In Gamemaker Studio 2 Not Moving - game-maker

My gun's begin step code:
if (global.night == true) {
image_angle = point_direction(x, y, mouse_x, mouse_y);
image_index = 1;
alarm[0] = 0.5 * room_speed;
firingdelay -= 1;
if (mouse_check_button_pressed(mb_left)) && (firingdelay < 0) {
firingdelay = 10;
with (instance_create_layer(Revolver.x+12, Revolver.y, "Bullets", Bullet)) {
direction = Revolver.image_angle;
speed = 25;
image_angle = direction;
}
}
}
It creates the bullets, but they don't move. Can anyone help?

If you have physics enabled in a room, the regular speed and direction variables will not work. Do you have physics enabled? The code looks fine.

Related

Collision GameMaker issue

I'm new to GameMaker and I followed a tutorial by "Let's Learn This Together". I was trying to make collisions for my character, however every time my character bumps into the object, they get stuck and I can't control them anymore. I checked many times and I tried my own ideas, but nothing worked.
if(keyboard_check(ord("D")) && place_free(x + collisionSpeed, y)) {
x += walkSpeed;
image_speed = walkSpeed / 3;
sprite_index = sClaire_Side_Right;
}
if(keyboard_check(ord("A")) && place_free(x - collisionSpeed, y)) {
x -= walkSpeed;
image_speed = walkSpeed / 3;
sprite_index = sClaire_Side_Left;
}
if(keyboard_check(ord("W")) && place_free(x, y - collisionSpeed)) {
y -= walkSpeed;
image_speed = walkSpeed / 3;
sprite_index = sClaire_Normal_Back;
}
if(keyboard_check(ord("S")) && place_free(x, y + collisionSpeed)) {
y += walkSpeed;
image_speed = walkSpeed / 3;
sprite_index = sClaire_Normal;
}
if(keyboard_check(vk_shift)) {
walkSpeed = 7;
}
if(keyboard_check(vk_nokey)) {
image_speed = 0;
image_index = 0;
walkSpeed = 3.5;
}
If you're moving instnace by walkSpeed, you should also check for collision in distance of walkSpeed, not collisionSpeed.
If collisionSpeed is smaller than walkSpeed by just 1 pixel, then it might return no collision as it will be last pixel before collision happens.
A good example is when walkSpeed=3, and collisionSpeed=2, which will for sure causes instance to stuck inside solid objects.

Collision detection is not working correctly

I have to submit a Breakout clone and I'm struggling with the collision detection of the ball and the bricks. Basically, the collision detection works, but the ball destroys the brick about 10 pixels away from the visual object. I'm checking the bounds of both objects, but I guess the problem is that the ball is a moving object and the brick is a static one.
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
brick = brickArray[i][j];
if (brick == null)
continue;
areBricksLeft = true;
Bounds brickBounds = brick.getBoundsInParent();
Bounds ballBounds = ball.getBoundsInParent();
if (brickBounds.intersects(ballBounds) ) {
brick.removeBrickAt(i, j, brick, brickArray, brickPane);
didHitBrick = true;
}
}
}
Thanks for the hint I found the mistake. I replaced my condition with this:
double ballX = ball.getLayoutX() + ball.getRadius();
double ballY = ball.getLayoutY() + ball.getRadius();
if ((ballX <= brickBounds.getMaxX() - 10 && ballX >= brickBounds.getMinX() -10) &&
(ballY <= brickBounds.getMaxY() - 10 && ballY >= brickBounds.getMinY() - 10)) {
brick.removeBrickAt(i, j, brick, brickArray, brickPane);
didHitBrick = true;
}
Now it is possible to adjust the collision by substracting and adding values to the bounds.

How to keep my QMainWindow always inside of the desktop?

I want to keep my QMainWindow always inside of the desktop, so I add the implementation for QMainWindow::moveEvent :
void MainWindow::moveEvent(QMoveEvent *ev)
{
if(ev->pos().x() < 0) setGeometry(0, ev->oldPos().y(), width(), height());
}
But when I move the window to out of desktop left bounding, the app is crashed.
What is wrong with this code? Why it is crashed? Is my solution correct?
//--Update:
I tried with this:
int newx = ev->pos().x(),
newy = ev->pos().y();
if(ev->pos().x() < 0) newx = 0;
if(ev->pos().y() < 0) newy = 0;
move(newx, newy);
It worked without crash but I'm not satisfied because of the moving is not smooth.
This should smoothly help with the upper left corner .. but you'll need to add some more conditions to get it working for all four sides.
posX and posY are member variables.
void MainWindow::moveStep() { // [SLOT]
int movX = 0, movY = 0;
if(posX < 0) movX = 1;
if(posY < 0) movY = 1;
move(posX + movX, posY + movY);
}
void MainWindow::moveEvent(QMoveEvent *ev) {
if(ev->pos().x() < 0 || ev->pos().y() < 0) {
posX = ev->pos().x();
posY = ev->pos().y();
QTimer::singleShot(10, this, SLOT(moveStep()));
}
}
To have it even more elegantly consider using QVariantAnimation with a QRect and setGeometry().

ios 8.1 calling AudioConvertFillComplexBuffer make a infinite loop with cpuload 100%

I'm converting audio by using the AudioToolBox.AudioConverter library.
AudioStreamBasicDescription inDescription;
inDescription.mSampleRate = 44100;
inDescription.mFormatID = kAudioFormatMPEGLayer3;
inDescription.mFormatFlags = 0;
inDescription.mBytesPerPacket = 0;
inDescription.mFramesPerPacket = 1152;
inDescription.mBytesPerFrame = 0;
inDescription.mChannelsPerFrame = 2;
inDescription.mBitsPerChannel = 0;
inDescription.mReserved = 0;
AudioStreamBasicDescription outDescription;
audioDescription.mFormatID = kAudioFormatLinearPCM
audioDescription.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked | kAudioFormatFlagsNativeEndian;
audioDescription.mChannelsPerFrame = 2;
audioDescription.mBytesPerPacket = sizeof(SInt16)*audioDescription.mChannelsPerFrame;
audioDescription.mFramesPerPacket = 1;
audioDescription.mBytesPerFrame = sizeof(SInt16)*audioDescription.mChannelsPerFrame;
audioDescription.mBitsPerChannel = 8 * sizeof(SInt16);
audioDescription.mSampleRate = 44100.0;
This is the conversion part with mp3AudioData...
AudioBufferList *mp3Audio = (AudioBufferList *)malloc(sizeof(AudioBufferList) + sizeof(AudioBuffer));
mp3Audio->mNumberBuffers = 1;
mp3Audio->mBuffers[0].mNumberChannels = 2;
mp3Audio->mBuffers[0].mDataByteSize = chunkLen;
mp3Audio->mBuffers[0].mData = calloc(chunkLen, sizeof(uint8_t));
memcpy(mp3Audio->mBuffers[0].mData, chunkData, chunkLen);
AudioStreamPacketDescription *packetDescription =
(AudioStreamPacketDescription*)malloc(sizeof(AudioStreamPacketDescription) * frames);
packetDescription->mDataByteSize = chunkLen;
packetDescription->mStartOffset = 0;
packetDescription->mVariableFramesInPacket = 1;
OSStatus result = AudioConverterFillComplexBuffer(audioConverter,
fillComplexBufferInputProc,
&(struct fillComplexBufferInputProc_t) { .bufferList =mp3Audio, .frames = frames, .packetDescriptions = packetDescription },
&frames,
pcmAudio,
NULL);
free(mp3Audio->mBuffers[0].mData);
free(mp3Audio);
free(packetDescription);
The first running of AudioConverterFillComplexBuffer is OK but when I run that function a second time, the infinite loop happens with cpuload 100%.
This code is OK on an IOS 7 device, but when I run it on an IOS 8 device, the infinite loop happens.
Could anyone tell me why this happens on IOS 8?
fillComplexBufferInputProc function:
static OSStatus fillComplexBufferInputProc(AudioConverterRef inAudioConverter,
UInt32 *ioNumberDataPackets,
AudioBufferList *ioData,
AudioStreamPacketDescription **outDataPacketDescription,
void *inUserData) {
struct fillComplexBufferInputProc_t *arg = inUserData;
for (int i = 0; i < ioData->mNumberBuffers; i++) {
ioData->mBuffers[i].mData = arg->bufferList->mBuffers[i].mData;
ioData->mBuffers[i].mDataByteSize = arg->bufferList->mBuffers[i].mDataByteSize;
}
if (NULL == *outDataPacketDescription) {
*outDataPacketDescription = arg->packetDescriptions;
}
*ioNumberDataPackets = arg->frames;
return noErr;
}
Because it seems no possible infinite loop in your code (except for-loop in fillComplexBufferInputProc function), I suggest you use Instruments (Time Profiler) to see what method is using the majority of CPU time. Check the stack trace for more information, so maybe we can help. Here is a tutorial for time profiler.

Why is this object in game maker sticking in the wall?

For whatever reason in gamemaker whenever this object (circle) is moving to the left (-x) it will stick into the wall, but when it moves to the right it acts as it should. I'll include pictures of the problem, along with the relevant code. The ball has gravity, and falls properly when on the right wall, but stays in the air on the left wall.
The code is a function that handles gravity/physics for anything that calls it. The top half (Right after isPlayer) is to handle the players gravity, so that can be ignored, but after the "else" is where the relevant code starts
Ball not stuck in wall
Ball stuck in wall
And here is the relevant code:
/*
runPhys(collision object, phys object, xVil, yVil, grav, environment x, environment y, isPlayer(1=player))
*/
obj=argument0;
physObj=argument1;
xVal=argument2;
yVal=argument3;
grav=argument4;
xEnv=argument5;
yEnv=argument6;
isPlayer=argument7;
with physObj{
if(isPlayer = 1){ //can ignore from here to
ySpeed = ySpeed + yVal + grav;
xAcc = xAcc+xVal;
if(xAcc>5){
xAcc = 5;
}
if(xAcc<-5){
xAcc = -5;
}
if(position_meeting(x,y+14,obj)){
xSpeed = 0;
xAcc = 0;
if(xVal = -0.2){
xAcc = -5;
}
if(xVal = 0.2){
xAcc = 5;
}
}
xSpeed = xAcc;
if(xSpeed>0){
if((!position_meeting(x+xSpeed+6,y,obj))&&(!position_meeting(x+xSpeed+6,y+12,obj))&&(!position_meeting(x+xSpeed+6,y-12,obj))){
x = x + xSpeed;
}else{
xSpeed = 0;
xAcc = 0;
}
}else{
if((!position_meeting(x-xSpeed-12,y,obj))&&(!position_meeting(x-xSpeed-12,y+12,obj))&&(!position_meeting(x-xSpeed-12,y-12,obj))){
x = x + xSpeed;
}else{
xSpeed = 0;
xAcc = 0;
}
}
if(ySpeed>0){
if(!position_meeting(x,y+ySpeed+12,obj)){
y = y + ySpeed;
}else{
ySpeed = 0;
}
}else{
if(!position_meeting(x,y-ySpeed-24,obj)){
y = y + ySpeed;
}else{
ySpeed = 0;
}
}
if(position_meeting(x,y+12,obj)){
y = y-1;
}
xSpeed = xSpeed/1.02;
}else{ //here, everything above here can be ignored
ySpeed = ySpeed + yVal + grav;
xSpeed = xSpeed + xVal;
if(xSpeed>0){ //the if below checks 3 positions according to the objects bounding box, in this case its checking the right top, right middle, and right bottom of the sprite
if((!position_meeting(x+xSpeed+(bbox_right-bbox_left)/2,y,obj))&&(!position_meeting(x+xSpeed+(bbox_right-bbox_left)/2,y+(bbox_bottom-bbox_top)/2,obj))&&(!position_meeting(x+xSpeed+(bbox_right-bbox_left)/2,y-(bbox_bottom-bbox_top)/2,obj))){
x = x + xSpeed;
}else{
xSpeed = 0;
xAcc = 0;
}
}else{ //this is where I believe the error to be. The above code handles moving to the right, and the below code handles moving to the left, where my problem is.
if((!position_meeting(x-xSpeed-((bbox_right-bbox_left)/2),y,obj))&&(!position_meeting(x-(xSpeed*2)-((bbox_right-bbox_left)/2),y+(bbox_bottom-bbox_top)/2,obj))&&(!position_meeting(x-(xSpeed*2)-((bbox_right-bbox_left)/2),y-(bbox_bottom-bbox_top)/2,obj))){
x = x + xSpeed;
}else{
xSpeed = 0;
xAcc = 0;
}
}
if(ySpeed>0){ //this is all y movement
if((!position_meeting(x,y+ySpeed+(bbox_bottom-bbox_top)/2,obj))&&(!position_meeting(x+(bbox_right-bbox_left)/2,y+ySpeed+(bbox_bottom-bbox_top)/2,obj))&&(!position_meeting(x-(bbox_right-bbox_left)/2,y+ySpeed+(bbox_bottom-bbox_top)/2,obj))){
y = y + ySpeed;
}else{
ySpeed = 0;
xSpeed = 0;
}
}else{
if((!position_meeting(x,y-ySpeed-(bbox_bottom-bbox_top)/2,obj))&&(!position_meeting(x+(bbox_right-bbox_left)/2,y-ySpeed-(bbox_bottom-bbox_top)/2,obj))&&(!position_meeting(x-(bbox_right-bbox_left)/2,y-ySpeed-(bbox_bottom-bbox_top)/2,obj))){
y = y + ySpeed;
}else{
ySpeed = 0;
xSpeed = 0;
}
}
if(position_meeting(x,y+(bbox_bottom-bbox_top)/2,obj)){
y = y-1;
xSpeed = 0;
}
}
}
I am going to make an educated guess based on what I am seeing. I do not know what your bounding boxes sizes are.
Your code checking for anything to the right of the object is checking a little too far to the left.
Your code checking towards the left of your object isn't checking far enough. If your objects speed is 7 and you are only checking 6 pixels to the left of the object, then you can end up inside the wall, which will allow your code checking to the right to detect a wall.
If you are checking for only certain points and not bounding boxes, then it is possible you are simply passing the object without detecting it, depending on the walls width. I would use collision_rectangle for this sort of thing.
Also I would learn the basics of encapsulation and seperation of concerns. By putting your code into separate functions you make it a lot easier to debug and to read.

Resources