Function `frame` does not return current frame

The following script is on my sprite. It moves correctly horizontally across the screen, and then flips to the next sprite. The frame always reports 0, though, so the sprite never moves back the other direction as intended.

on enter do
	log "enter"
	tell event.x,event.y to
		call "update_bug"
	end
end

on swap_frame do
  current_frame = frame
	if current_frame==0 then
		frame 1
	else
		frame 0
	end
	tell event.x,event.y to
		call "update_bug"
	end
end

on update_bug do
	bug_x = event.x
	bug_y = event.y
	
	current_frame = frame
	
	log "current frame {current_frame}"
	
	target_x = bug_x
	if current_frame==0 then
		target_x += 1
	else
		target_x -= 1
	end
	
	target_y = bug_y
	
	target_tile = name target_x,target_y
	bug_tile = name bug_x,bug_y
	
	if target_tile!="black" then
		wait 1 then
			tell bug_x,bug_y to
				swap "white"
			end
			
			tell target_x,target_y to
				swap bug_tile
				call "update_bug"
			end
		end
	else
		wait 1 then
			tell bug_x,bug_y to
				call "swap_frame"
			end
		end
	end
end

Am I misunderstanding the use of the frame function?

This looks like (appropriately!) a bug with 0fps tiles. Until it can be fixed I would suggest either maintaining your own variable that you update when you change the sprite’s frame or splitting the bug into “bug left” and “bug right” sprites and use swap instead of frame to change its direction. One additional benefit of the separate sprite approach is you can animate the bug.

To cut down on redundant PulpScript you could have the following on the “bug right” sprite:

on any do
    mimic "bug left"
end

and move your movement code to a separate event handler and then override that in “bug right”.

So in “bug_left” you’d have:

on update_pos do // expects target_x
    target_x -= 1
end

And in “bug_right”:

on update_pos do // expects target_x
    target_x += 1
end

And in the update_bug handler of “bug left” change:

target_x = bug_x
if current_frame==0 then
	target_x += 1
else
	target_x -= 1
end

to

target_x = bug_x
call "update_pos"